Skip to content

Commit

Permalink
Fix PHPStan
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar committed Jun 19, 2024
1 parent 95a910d commit 3083a0b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 32 deletions.
21 changes: 19 additions & 2 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Orisai\VFS\Structure\RootDirectory;
use Orisai\VFS\Wrapper\PermissionChecker;
use RuntimeException;
use function array_filter;
use function basename;
use function clearstatcache;
use function dirname;
Expand Down Expand Up @@ -51,13 +50,31 @@ public function getRootDirectory(): RootDirectory
return $this->root;
}

/**
* @return list<string>
*/
private function getPathParts(string $path): array
{
$path = str_replace('\\', '/', $path);

Check warning on line 58 in src/Container.php

View workflow job for this annotation

GitHub Actions / Test for mutants (ubuntu-latest, 8.1)

Escaped Mutant for Mutator "UnwrapStrReplace": --- Original +++ New @@ @@ */ private function getPathParts(string $path) : array { - $path = str_replace('\\', '/', $path); + $path = $path; $pathParts = explode('/', $path); $filteredPathParts = []; foreach ($pathParts as $part) {
$pathParts = explode('/', $path);
$filteredPathParts = [];

foreach ($pathParts as $part) {
if ($part !== '') {
$filteredPathParts[] = $part;
}
}

return $filteredPathParts;
}

/**
* @return Directory|File|Link
* @throws PathNotFound
*/
public function getNodeAt(string $path): Node
{
$pathParts = array_filter(explode('/', str_replace('\\', '/', $path)), 'strlen');
$pathParts = $this->getPathParts($path);

$node = $this->getRootDirectory();

Expand Down
45 changes: 37 additions & 8 deletions tests/Unit/VfsStreamWrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,10 @@ public function testChownByName(): void
);

chown("$this->scheme://", 'root');
self::assertSame('root', posix_getpwuid(fileowner("$this->scheme://"))['name']);
$userInfo = posix_getpwuid(fileowner("$this->scheme://"));
self::assertNotFalse($userInfo);

self::assertSame('root', $userInfo['name']);
}

public function testChownById(): void
Expand Down Expand Up @@ -231,11 +234,22 @@ public function testChgrpByName(): void

//lets workout available group
//this is needed to find string name of group root belongs to
$group = posix_getgrgid(posix_getpwuid(0)['gid'])['name'];
$oldUserInfo = posix_getpwuid(0);
self::assertNotFalse($oldUserInfo);

chgrp("$this->scheme://", $group);
$oldGroupInfo = posix_getgrgid($oldUserInfo['gid']);
self::assertNotFalse($oldGroupInfo);

$groupName = $oldGroupInfo['name'];

chgrp("$this->scheme://", $groupName);

$userInfo = posix_getpwuid(0);
self::assertNotFalse($userInfo);
$groupInfo = posix_getgrgid($userInfo['gid']);
self::assertNotFalse($groupInfo);

self::assertSame($group, posix_getgrgid(filegroup("$this->scheme://"))['name']);
self::assertSame($groupName, $groupInfo['name']);
}

public function testChgrpById(): void
Expand All @@ -252,7 +266,10 @@ public function testChgrpById(): void
);

//lets workout available group
$group = posix_getpwuid(0)['gid'];
$userInfo = posix_getpwuid(0);
self::assertNotFalse($userInfo);

$group = $userInfo['gid'];

chgrp("$this->scheme://", $group);

Expand Down Expand Up @@ -1332,7 +1349,11 @@ public function testLchown(): void
);

lchown("$this->scheme://dir/link", 'root');
self::assertSame('root', posix_getpwuid(fileowner("$this->scheme://dir/link"))['name']);

$userInfo = posix_getpwuid(fileowner("$this->scheme://dir/link"));
self::assertNotFalse($userInfo);

self::assertSame('root', $userInfo['name']);
}

public function testLchgrp(): void
Expand All @@ -1355,11 +1376,19 @@ public function testLchgrp(): void

//lets workout available group
//this is needed to find string name of group root belongs to
$group = posix_getgrgid(posix_getpwuid(0)['gid'])['name'];
$userInfo = posix_getpwuid(0);
self::assertNotFalse($userInfo);

$oldGroupInfo = posix_getgrgid($userInfo['gid']);
self::assertNotFalse($oldGroupInfo);

$group = $oldGroupInfo['name'];

chgrp("$this->scheme://dir/link", $group);
$groupInfo = posix_getgrgid(filegroup("$this->scheme://dir/link"));
self::assertNotFalse($groupInfo);

self::assertSame($group, posix_getgrgid(filegroup("$this->scheme://dir/link"))['name']);
self::assertSame($group, $groupInfo['name']);
}

public function testFileCopy(): void
Expand Down
17 changes: 1 addition & 16 deletions tools/phpstan.baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,11 @@ parameters:
count: 2
path: ../tests/Unit/VfsStreamWrapperTest.php

-
message: "#^Cannot access offset 'gid' on array\\|false\\.$#"
count: 3
path: ../tests/Unit/VfsStreamWrapperTest.php

-
message: "#^Cannot access offset 'mtime' on array\\{0\\: int, 1\\: int, 2\\: int, 3\\: int, 4\\: int, 5\\: int, 6\\: int, 7\\: int, \\.\\.\\.\\}\\|false\\.$#"
count: 3
path: ../tests/Unit/VfsStreamWrapperTest.php

-
message: "#^Cannot access offset 'name' on array\\{name\\: string, passwd\\: string, gid\\: int, members\\: list\\<string\\>\\}\\|false\\.$#"
count: 4
path: ../tests/Unit/VfsStreamWrapperTest.php

-
message: "#^Cannot access offset 'name' on array\\|false\\.$#"
count: 2
path: ../tests/Unit/VfsStreamWrapperTest.php

-
message: "#^Offset 'message' might not exist on array\\{type\\: int, message\\: string, file\\: string, line\\: int\\}\\|null\\.$#"
count: 33
Expand Down Expand Up @@ -72,7 +57,7 @@ parameters:

-
message: "#^Parameter \\#1 \\$gid of function posix_getgrgid expects int, int\\|false given\\.$#"
count: 2
count: 1
path: ../tests/Unit/VfsStreamWrapperTest.php

-
Expand Down
6 changes: 0 additions & 6 deletions tools/phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ parameters:
# If it fails, I will fix it
- '#^(.+)string\|false(.+)$#'

# It has, we are testing access time
-
message: '#^Call to function file_get_contents\(\) on a separate line has no effect\.$#'
path: ../tests/Unit/VfsStreamWrapperTest.php
count: 1

# It *should*, we are testing internal behavior
-
message: '#^Call to static method (.+)\:\:assertFalse\(\) with true and ''(.+)'' will always evaluate to false\.$#'
Expand Down

0 comments on commit 3083a0b

Please sign in to comment.