From 3083a0b3e046a518c8257d4b3efaf003fd0cd201 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Barto=C5=A1?= Date: Wed, 19 Jun 2024 11:22:17 +0200 Subject: [PATCH] Fix PHPStan --- src/Container.php | 21 ++++++++++++-- tests/Unit/VfsStreamWrapperTest.php | 45 ++++++++++++++++++++++++----- tools/phpstan.baseline.neon | 17 +---------- tools/phpstan.neon | 6 ---- 4 files changed, 57 insertions(+), 32 deletions(-) diff --git a/src/Container.php b/src/Container.php index 3ba118d..377bfc0 100644 --- a/src/Container.php +++ b/src/Container.php @@ -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; @@ -51,13 +50,31 @@ public function getRootDirectory(): RootDirectory return $this->root; } + /** + * @return list + */ + private function getPathParts(string $path): array + { + $path = str_replace('\\', '/', $path); + $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(); diff --git a/tests/Unit/VfsStreamWrapperTest.php b/tests/Unit/VfsStreamWrapperTest.php index 0e3b31a..1c33170 100644 --- a/tests/Unit/VfsStreamWrapperTest.php +++ b/tests/Unit/VfsStreamWrapperTest.php @@ -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 @@ -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 @@ -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); @@ -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 @@ -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 diff --git a/tools/phpstan.baseline.neon b/tools/phpstan.baseline.neon index cfb6050..d7153c0 100644 --- a/tools/phpstan.baseline.neon +++ b/tools/phpstan.baseline.neon @@ -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\\\\}\\|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 @@ -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 - diff --git a/tools/phpstan.neon b/tools/phpstan.neon index bb17795..7ad48e9 100644 --- a/tools/phpstan.neon +++ b/tools/phpstan.neon @@ -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\.$#'