diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index 285107f2..d96ae7e8 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -7,7 +7,8 @@ ->in('tests') ->exclude([ 'Workspace', - 'Integration/Composer/project' + 'Integration/Composer/project', + 'Integration/Simple/project' ]) ; diff --git a/composer.json b/composer.json index dcd9cefd..075d817c 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ "require-dev": { "ergebnis/composer-normalize": "^2.0", "friendsofphp/php-cs-fixer": "^3.0", - "phpstan/phpstan": "~0.12.0", + "phpstan/phpstan": "^1.0", "phpunit/phpunit": "^9.0", "phpspec/prophecy-phpunit": "^2.0", "symfony/var-dumper": "^6.0 || ^5.0" diff --git a/lib/Adapter/Simple/ClassScanner.php b/lib/Adapter/Simple/ClassScanner.php index a4cbfaac..9300ae0f 100644 --- a/lib/Adapter/Simple/ClassScanner.php +++ b/lib/Adapter/Simple/ClassScanner.php @@ -55,7 +55,7 @@ public function getClassNameFromFile(string $file): ?string if ($this->isClassLike($tokens[$i])) { for ($j = $i + 1; $j < count($tokens); $j++) { - if ($tokens[$j][0] === T_STRING) { + if ($tokens[$j][0] === T_STRING && isset($tokens[$i + 2][1])) { $class = $tokens[$i + 2][1]; break 2; diff --git a/lib/Adapter/Simple/SimpleClassToFile.php b/lib/Adapter/Simple/SimpleClassToFile.php index 1c16d41b..a12240d5 100644 --- a/lib/Adapter/Simple/SimpleClassToFile.php +++ b/lib/Adapter/Simple/SimpleClassToFile.php @@ -43,7 +43,7 @@ public function classToFileCandidates(ClassName $className): FilePathCandidates foreach ($iterator as $phpFile) { $scannedClass = $this->classScanner->getClassNameFromFile($phpFile->getPathName()); - if (ClassName::fromString($scannedClass) == $className) { + if ($scannedClass && ClassName::fromString($scannedClass) == $className) { $candidates[] = FilePath::fromString($phpFile->getPathName()); } } diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 8c78cbce..3be70490 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1,12 +1,7 @@ parameters: ignoreErrors: - - message: "#^Method Phpactor\\\\ClassFileConverter\\\\Adapter\\\\Composer\\\\ComposerClassToFile\\:\\:classToFileCandidates\\(\\) return type has no value type specified in iterable type Phpactor\\\\ClassFileConverter\\\\Domain\\\\FilePathCandidates\\.$#" - count: 1 - path: lib/Adapter/Composer/ComposerClassToFile.php - - - - message: "#^Method Phpactor\\\\ClassFileConverter\\\\Adapter\\\\Composer\\\\ComposerClassToFile\\:\\:getFileCandidates\\(\\) has no return typehint specified\\.$#" + message: "#^Method Phpactor\\\\ClassFileConverter\\\\Adapter\\\\Composer\\\\ComposerClassToFile\\:\\:getFileCandidates\\(\\) has no return type specified\\.$#" count: 1 path: lib/Adapter/Composer/ComposerClassToFile.php @@ -21,7 +16,7 @@ parameters: path: lib/Adapter/Composer/ComposerClassToFile.php - - message: "#^Method Phpactor\\\\ClassFileConverter\\\\Adapter\\\\Composer\\\\ComposerClassToFile\\:\\:resolveFile\\(\\) has parameter \\$candidates with no typehint specified\\.$#" + message: "#^Method Phpactor\\\\ClassFileConverter\\\\Adapter\\\\Composer\\\\ComposerClassToFile\\:\\:resolveFile\\(\\) has parameter \\$candidates with no type specified\\.$#" count: 1 path: lib/Adapter/Composer/ComposerClassToFile.php @@ -30,18 +25,13 @@ parameters: count: 1 path: lib/Adapter/Composer/ComposerClassToFile.php - - - message: "#^Method Phpactor\\\\ClassFileConverter\\\\Adapter\\\\Composer\\\\ComposerFileToClass\\:\\:fileToClassCandidates\\(\\) return type has no value type specified in iterable type Phpactor\\\\ClassFileConverter\\\\Domain\\\\ClassNameCandidates\\.$#" - count: 1 - path: lib/Adapter/Composer/ComposerFileToClass.php - - message: "#^Method Phpactor\\\\ClassFileConverter\\\\Adapter\\\\Composer\\\\ComposerFileToClass\\:\\:getStrategies\\(\\) return type has no value type specified in iterable type array\\.$#" count: 1 path: lib/Adapter/Composer/ComposerFileToClass.php - - message: "#^Method Phpactor\\\\ClassFileConverter\\\\Adapter\\\\Composer\\\\ComposerFileToClass\\:\\:populateCandidates\\(\\) has no return typehint specified\\.$#" + message: "#^Method Phpactor\\\\ClassFileConverter\\\\Adapter\\\\Composer\\\\ComposerFileToClass\\:\\:populateCandidates\\(\\) has no return type specified\\.$#" count: 1 path: lib/Adapter/Composer/ComposerFileToClass.php @@ -50,28 +40,18 @@ parameters: count: 1 path: lib/Adapter/Composer/ComposerFileToClass.php - - - message: "#^Method Phpactor\\\\ClassFileConverter\\\\Adapter\\\\Simple\\\\SimpleClassToFile\\:\\:classToFileCandidates\\(\\) return type has no value type specified in iterable type Phpactor\\\\ClassFileConverter\\\\Domain\\\\FilePathCandidates\\.$#" - count: 1 - path: lib/Adapter/Simple/SimpleClassToFile.php - - - - message: "#^Method Phpactor\\\\ClassFileConverter\\\\Adapter\\\\Simple\\\\SimpleFileToClass\\:\\:fileToClassCandidates\\(\\) return type has no value type specified in iterable type Phpactor\\\\ClassFileConverter\\\\Domain\\\\ClassNameCandidates\\.$#" - count: 1 - path: lib/Adapter/Simple/SimpleFileToClass.php - - message: "#^Method Phpactor\\\\ClassFileConverter\\\\ClassToFileConverter\\:\\:fromComposerAutoloaders\\(\\) has parameter \\$classLoaders with no value type specified in iterable type array\\.$#" count: 1 path: lib/ClassToFileConverter.php - - message: "#^Method Phpactor\\\\ClassFileConverter\\\\Domain\\\\ChainClassToFile\\:\\:__construct\\(\\) has parameter \\$converters with no value type specified in iterable type array\\.$#" + message: "#^Property Phpactor\\\\ClassFileConverter\\\\ClassToFileConverter\\:\\:\\$converter is never read, only written\\.$#" count: 1 - path: lib/Domain/ChainClassToFile.php + path: lib/ClassToFileConverter.php - - message: "#^Method Phpactor\\\\ClassFileConverter\\\\Domain\\\\ChainClassToFile\\:\\:classToFileCandidates\\(\\) return type has no value type specified in iterable type Phpactor\\\\ClassFileConverter\\\\Domain\\\\FilePathCandidates\\.$#" + message: "#^Method Phpactor\\\\ClassFileConverter\\\\Domain\\\\ChainClassToFile\\:\\:__construct\\(\\) has parameter \\$converters with no value type specified in iterable type array\\.$#" count: 1 path: lib/Domain/ChainClassToFile.php @@ -85,11 +65,6 @@ parameters: count: 1 path: lib/Domain/ChainFileToClass.php - - - message: "#^Method Phpactor\\\\ClassFileConverter\\\\Domain\\\\ChainFileToClass\\:\\:fileToClassCandidates\\(\\) return type has no value type specified in iterable type Phpactor\\\\ClassFileConverter\\\\Domain\\\\ClassNameCandidates\\.$#" - count: 1 - path: lib/Domain/ChainFileToClass.php - - message: "#^Property Phpactor\\\\ClassFileConverter\\\\Domain\\\\ChainFileToClass\\:\\:\\$converters type has no value type specified in iterable type array\\.$#" count: 1 @@ -106,7 +81,7 @@ parameters: path: lib/Domain/ClassNameCandidates.php - - message: "#^Method Phpactor\\\\ClassFileConverter\\\\Domain\\\\ClassNameCandidates\\:\\:fromClassNames\\(\\) has no return typehint specified\\.$#" + message: "#^Method Phpactor\\\\ClassFileConverter\\\\Domain\\\\ClassNameCandidates\\:\\:fromClassNames\\(\\) has no return type specified\\.$#" count: 1 path: lib/Domain/ClassNameCandidates.php @@ -116,37 +91,17 @@ parameters: path: lib/Domain/ClassNameCandidates.php - - message: "#^Method Phpactor\\\\ClassFileConverter\\\\Domain\\\\ClassNameCandidates\\:\\:getIterator\\(\\) return type has no value type specified in iterable type Traversable\\\\.$#" + message: "#^Property Phpactor\\\\ClassFileConverter\\\\Domain\\\\ClassNameCandidates\\:\\:\\$classNames has no type specified\\.$#" count: 1 path: lib/Domain/ClassNameCandidates.php - - - message: "#^Property Phpactor\\\\ClassFileConverter\\\\Domain\\\\ClassNameCandidates\\:\\:\\$classNames has no typehint specified\\.$#" - count: 1 - path: lib/Domain/ClassNameCandidates.php - - - - message: "#^Method Phpactor\\\\ClassFileConverter\\\\Domain\\\\ClassToFile\\:\\:classToFileCandidates\\(\\) return type has no value type specified in iterable type Phpactor\\\\ClassFileConverter\\\\Domain\\\\FilePathCandidates\\.$#" - count: 1 - path: lib/Domain/ClassToFile.php - - - - message: "#^Method Phpactor\\\\ClassFileConverter\\\\Domain\\\\ClassToFileFileToClass\\:\\:classToFileCandidates\\(\\) return type has no value type specified in iterable type Phpactor\\\\ClassFileConverter\\\\Domain\\\\FilePathCandidates\\.$#" - count: 1 - path: lib/Domain/ClassToFileFileToClass.php - - - - message: "#^Method Phpactor\\\\ClassFileConverter\\\\Domain\\\\ClassToFileFileToClass\\:\\:fileToClassCandidates\\(\\) return type has no value type specified in iterable type Phpactor\\\\ClassFileConverter\\\\Domain\\\\ClassNameCandidates\\.$#" - count: 1 - path: lib/Domain/ClassToFileFileToClass.php - - message: "#^Method Phpactor\\\\ClassFileConverter\\\\Domain\\\\FilePath\\:\\:fromParts\\(\\) has parameter \\$parts with no value type specified in iterable type array\\.$#" count: 1 path: lib/Domain/FilePath.php - - message: "#^Method Phpactor\\\\ClassFileConverter\\\\Domain\\\\FilePath\\:\\:fromString\\(\\) has parameter \\$path with no typehint specified\\.$#" + message: "#^Method Phpactor\\\\ClassFileConverter\\\\Domain\\\\FilePath\\:\\:fromString\\(\\) has parameter \\$path with no type specified\\.$#" count: 1 path: lib/Domain/FilePath.php @@ -161,12 +116,12 @@ parameters: path: lib/Domain/FilePathCandidates.php - - message: "#^Method Phpactor\\\\ClassFileConverter\\\\Domain\\\\FilePathCandidates\\:\\:create\\(\\) has no return typehint specified\\.$#" + message: "#^Method Phpactor\\\\ClassFileConverter\\\\Domain\\\\FilePathCandidates\\:\\:create\\(\\) has no return type specified\\.$#" count: 1 path: lib/Domain/FilePathCandidates.php - - message: "#^Method Phpactor\\\\ClassFileConverter\\\\Domain\\\\FilePathCandidates\\:\\:fromFilePaths\\(\\) has no return typehint specified\\.$#" + message: "#^Method Phpactor\\\\ClassFileConverter\\\\Domain\\\\FilePathCandidates\\:\\:fromFilePaths\\(\\) has no return type specified\\.$#" count: 1 path: lib/Domain/FilePathCandidates.php @@ -175,23 +130,12 @@ parameters: count: 1 path: lib/Domain/FilePathCandidates.php - - - message: "#^Method Phpactor\\\\ClassFileConverter\\\\Domain\\\\FilePathCandidates\\:\\:getIterator\\(\\) return type has no value type specified in iterable type Traversable\\\\.$#" - count: 1 - path: lib/Domain/FilePathCandidates.php - - message: "#^Method Phpactor\\\\ClassFileConverter\\\\Domain\\\\FilePathCandidates\\:\\:toArray\\(\\) return type has no value type specified in iterable type array\\.$#" count: 1 path: lib/Domain/FilePathCandidates.php - - message: "#^Property Phpactor\\\\ClassFileConverter\\\\Domain\\\\FilePathCandidates\\:\\:\\$filePaths has no typehint specified\\.$#" + message: "#^Property Phpactor\\\\ClassFileConverter\\\\Domain\\\\FilePathCandidates\\:\\:\\$filePaths has no type specified\\.$#" count: 1 path: lib/Domain/FilePathCandidates.php - - - - message: "#^Method Phpactor\\\\ClassFileConverter\\\\Domain\\\\FileToClass\\:\\:fileToClassCandidates\\(\\) return type has no value type specified in iterable type Phpactor\\\\ClassFileConverter\\\\Domain\\\\ClassNameCandidates\\.$#" - count: 1 - path: lib/Domain/FileToClass.php - diff --git a/tests/Integration/Simple/SimpleClassToFileTest.php b/tests/Integration/Simple/SimpleClassToFileTest.php index 8a2b80b1..a3b07381 100644 --- a/tests/Integration/Simple/SimpleClassToFileTest.php +++ b/tests/Integration/Simple/SimpleClassToFileTest.php @@ -30,6 +30,16 @@ public function testClassToFile(): void ]), $candidates); } + public function testClassToFileInvalid(): void + { + $candidates = $this->classToFile->classToFileCandidates( + ClassName::fromString('InvalidClass') + ); + + $this->assertEquals(FilePathCandidates::fromFilePaths([ + ]), $candidates); + } + public function testClassToFileDeeper(): void { $candidates = $this->classToFile->classToFileCandidates(ClassName::fromString('Acme\\NamespaceHere\\Hallo')); diff --git a/tests/Integration/Simple/project/lib/InvalidClass.php b/tests/Integration/Simple/project/lib/InvalidClass.php new file mode 100644 index 00000000..5ac7857a --- /dev/null +++ b/tests/Integration/Simple/project/lib/InvalidClass.php @@ -0,0 +1,7 @@ +