Skip to content

Commit

Permalink
Fix case where scanned class name is NULL (#31)
Browse files Browse the repository at this point in the history
* Fix bug related to #920

* Update phpstan
  • Loading branch information
dantleech authored Oct 8, 2022
1 parent 0121674 commit 290dabd
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 72 deletions.
3 changes: 2 additions & 1 deletion .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
->in('tests')
->exclude([
'Workspace',
'Integration/Composer/project'
'Integration/Composer/project',
'Integration/Simple/project'
])
;

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion lib/Adapter/Simple/ClassScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion lib/Adapter/Simple/SimpleClassToFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Expand Down
80 changes: 12 additions & 68 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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
Expand All @@ -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

Expand All @@ -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\\<mixed, mixed\\>\\.$#"
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

Expand All @@ -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

Expand All @@ -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\\<mixed, mixed\\>\\.$#"
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

10 changes: 10 additions & 0 deletions tests/Integration/Simple/SimpleClassToFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
7 changes: 7 additions & 0 deletions tests/Integration/Simple/project/lib/InvalidClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Phpactor\ClassFileConverter\Tests\Integration\Simple\project\lib;

class
{
}

0 comments on commit 290dabd

Please sign in to comment.