Skip to content

Commit 290dabd

Browse files
authored
Fix case where scanned class name is NULL (#31)
* Fix bug related to #920 * Update phpstan
1 parent 0121674 commit 290dabd

File tree

7 files changed

+34
-72
lines changed

7 files changed

+34
-72
lines changed

.php-cs-fixer.dist.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
->in('tests')
88
->exclude([
99
'Workspace',
10-
'Integration/Composer/project'
10+
'Integration/Composer/project',
11+
'Integration/Simple/project'
1112
])
1213
;
1314

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"require-dev": {
1717
"ergebnis/composer-normalize": "^2.0",
1818
"friendsofphp/php-cs-fixer": "^3.0",
19-
"phpstan/phpstan": "~0.12.0",
19+
"phpstan/phpstan": "^1.0",
2020
"phpunit/phpunit": "^9.0",
2121
"phpspec/prophecy-phpunit": "^2.0",
2222
"symfony/var-dumper": "^6.0 || ^5.0"

lib/Adapter/Simple/ClassScanner.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function getClassNameFromFile(string $file): ?string
5555

5656
if ($this->isClassLike($tokens[$i])) {
5757
for ($j = $i + 1; $j < count($tokens); $j++) {
58-
if ($tokens[$j][0] === T_STRING) {
58+
if ($tokens[$j][0] === T_STRING && isset($tokens[$i + 2][1])) {
5959
$class = $tokens[$i + 2][1];
6060

6161
break 2;

lib/Adapter/Simple/SimpleClassToFile.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function classToFileCandidates(ClassName $className): FilePathCandidates
4343
foreach ($iterator as $phpFile) {
4444
$scannedClass = $this->classScanner->getClassNameFromFile($phpFile->getPathName());
4545

46-
if (ClassName::fromString($scannedClass) == $className) {
46+
if ($scannedClass && ClassName::fromString($scannedClass) == $className) {
4747
$candidates[] = FilePath::fromString($phpFile->getPathName());
4848
}
4949
}

phpstan-baseline.neon

Lines changed: 12 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
parameters:
22
ignoreErrors:
33
-
4-
message: "#^Method Phpactor\\\\ClassFileConverter\\\\Adapter\\\\Composer\\\\ComposerClassToFile\\:\\:classToFileCandidates\\(\\) return type has no value type specified in iterable type Phpactor\\\\ClassFileConverter\\\\Domain\\\\FilePathCandidates\\.$#"
5-
count: 1
6-
path: lib/Adapter/Composer/ComposerClassToFile.php
7-
8-
-
9-
message: "#^Method Phpactor\\\\ClassFileConverter\\\\Adapter\\\\Composer\\\\ComposerClassToFile\\:\\:getFileCandidates\\(\\) has no return typehint specified\\.$#"
4+
message: "#^Method Phpactor\\\\ClassFileConverter\\\\Adapter\\\\Composer\\\\ComposerClassToFile\\:\\:getFileCandidates\\(\\) has no return type specified\\.$#"
105
count: 1
116
path: lib/Adapter/Composer/ComposerClassToFile.php
127

@@ -21,7 +16,7 @@ parameters:
2116
path: lib/Adapter/Composer/ComposerClassToFile.php
2217

2318
-
24-
message: "#^Method Phpactor\\\\ClassFileConverter\\\\Adapter\\\\Composer\\\\ComposerClassToFile\\:\\:resolveFile\\(\\) has parameter \\$candidates with no typehint specified\\.$#"
19+
message: "#^Method Phpactor\\\\ClassFileConverter\\\\Adapter\\\\Composer\\\\ComposerClassToFile\\:\\:resolveFile\\(\\) has parameter \\$candidates with no type specified\\.$#"
2520
count: 1
2621
path: lib/Adapter/Composer/ComposerClassToFile.php
2722

@@ -30,18 +25,13 @@ parameters:
3025
count: 1
3126
path: lib/Adapter/Composer/ComposerClassToFile.php
3227

33-
-
34-
message: "#^Method Phpactor\\\\ClassFileConverter\\\\Adapter\\\\Composer\\\\ComposerFileToClass\\:\\:fileToClassCandidates\\(\\) return type has no value type specified in iterable type Phpactor\\\\ClassFileConverter\\\\Domain\\\\ClassNameCandidates\\.$#"
35-
count: 1
36-
path: lib/Adapter/Composer/ComposerFileToClass.php
37-
3828
-
3929
message: "#^Method Phpactor\\\\ClassFileConverter\\\\Adapter\\\\Composer\\\\ComposerFileToClass\\:\\:getStrategies\\(\\) return type has no value type specified in iterable type array\\.$#"
4030
count: 1
4131
path: lib/Adapter/Composer/ComposerFileToClass.php
4232

4333
-
44-
message: "#^Method Phpactor\\\\ClassFileConverter\\\\Adapter\\\\Composer\\\\ComposerFileToClass\\:\\:populateCandidates\\(\\) has no return typehint specified\\.$#"
34+
message: "#^Method Phpactor\\\\ClassFileConverter\\\\Adapter\\\\Composer\\\\ComposerFileToClass\\:\\:populateCandidates\\(\\) has no return type specified\\.$#"
4535
count: 1
4636
path: lib/Adapter/Composer/ComposerFileToClass.php
4737

@@ -50,28 +40,18 @@ parameters:
5040
count: 1
5141
path: lib/Adapter/Composer/ComposerFileToClass.php
5242

53-
-
54-
message: "#^Method Phpactor\\\\ClassFileConverter\\\\Adapter\\\\Simple\\\\SimpleClassToFile\\:\\:classToFileCandidates\\(\\) return type has no value type specified in iterable type Phpactor\\\\ClassFileConverter\\\\Domain\\\\FilePathCandidates\\.$#"
55-
count: 1
56-
path: lib/Adapter/Simple/SimpleClassToFile.php
57-
58-
-
59-
message: "#^Method Phpactor\\\\ClassFileConverter\\\\Adapter\\\\Simple\\\\SimpleFileToClass\\:\\:fileToClassCandidates\\(\\) return type has no value type specified in iterable type Phpactor\\\\ClassFileConverter\\\\Domain\\\\ClassNameCandidates\\.$#"
60-
count: 1
61-
path: lib/Adapter/Simple/SimpleFileToClass.php
62-
6343
-
6444
message: "#^Method Phpactor\\\\ClassFileConverter\\\\ClassToFileConverter\\:\\:fromComposerAutoloaders\\(\\) has parameter \\$classLoaders with no value type specified in iterable type array\\.$#"
6545
count: 1
6646
path: lib/ClassToFileConverter.php
6747

6848
-
69-
message: "#^Method Phpactor\\\\ClassFileConverter\\\\Domain\\\\ChainClassToFile\\:\\:__construct\\(\\) has parameter \\$converters with no value type specified in iterable type array\\.$#"
49+
message: "#^Property Phpactor\\\\ClassFileConverter\\\\ClassToFileConverter\\:\\:\\$converter is never read, only written\\.$#"
7050
count: 1
71-
path: lib/Domain/ChainClassToFile.php
51+
path: lib/ClassToFileConverter.php
7252

7353
-
74-
message: "#^Method Phpactor\\\\ClassFileConverter\\\\Domain\\\\ChainClassToFile\\:\\:classToFileCandidates\\(\\) return type has no value type specified in iterable type Phpactor\\\\ClassFileConverter\\\\Domain\\\\FilePathCandidates\\.$#"
54+
message: "#^Method Phpactor\\\\ClassFileConverter\\\\Domain\\\\ChainClassToFile\\:\\:__construct\\(\\) has parameter \\$converters with no value type specified in iterable type array\\.$#"
7555
count: 1
7656
path: lib/Domain/ChainClassToFile.php
7757

@@ -85,11 +65,6 @@ parameters:
8565
count: 1
8666
path: lib/Domain/ChainFileToClass.php
8767

88-
-
89-
message: "#^Method Phpactor\\\\ClassFileConverter\\\\Domain\\\\ChainFileToClass\\:\\:fileToClassCandidates\\(\\) return type has no value type specified in iterable type Phpactor\\\\ClassFileConverter\\\\Domain\\\\ClassNameCandidates\\.$#"
90-
count: 1
91-
path: lib/Domain/ChainFileToClass.php
92-
9368
-
9469
message: "#^Property Phpactor\\\\ClassFileConverter\\\\Domain\\\\ChainFileToClass\\:\\:\\$converters type has no value type specified in iterable type array\\.$#"
9570
count: 1
@@ -106,7 +81,7 @@ parameters:
10681
path: lib/Domain/ClassNameCandidates.php
10782

10883
-
109-
message: "#^Method Phpactor\\\\ClassFileConverter\\\\Domain\\\\ClassNameCandidates\\:\\:fromClassNames\\(\\) has no return typehint specified\\.$#"
84+
message: "#^Method Phpactor\\\\ClassFileConverter\\\\Domain\\\\ClassNameCandidates\\:\\:fromClassNames\\(\\) has no return type specified\\.$#"
11085
count: 1
11186
path: lib/Domain/ClassNameCandidates.php
11287

@@ -116,37 +91,17 @@ parameters:
11691
path: lib/Domain/ClassNameCandidates.php
11792

11893
-
119-
message: "#^Method Phpactor\\\\ClassFileConverter\\\\Domain\\\\ClassNameCandidates\\:\\:getIterator\\(\\) return type has no value type specified in iterable type Traversable\\<mixed, mixed\\>\\.$#"
94+
message: "#^Property Phpactor\\\\ClassFileConverter\\\\Domain\\\\ClassNameCandidates\\:\\:\\$classNames has no type specified\\.$#"
12095
count: 1
12196
path: lib/Domain/ClassNameCandidates.php
12297

123-
-
124-
message: "#^Property Phpactor\\\\ClassFileConverter\\\\Domain\\\\ClassNameCandidates\\:\\:\\$classNames has no typehint specified\\.$#"
125-
count: 1
126-
path: lib/Domain/ClassNameCandidates.php
127-
128-
-
129-
message: "#^Method Phpactor\\\\ClassFileConverter\\\\Domain\\\\ClassToFile\\:\\:classToFileCandidates\\(\\) return type has no value type specified in iterable type Phpactor\\\\ClassFileConverter\\\\Domain\\\\FilePathCandidates\\.$#"
130-
count: 1
131-
path: lib/Domain/ClassToFile.php
132-
133-
-
134-
message: "#^Method Phpactor\\\\ClassFileConverter\\\\Domain\\\\ClassToFileFileToClass\\:\\:classToFileCandidates\\(\\) return type has no value type specified in iterable type Phpactor\\\\ClassFileConverter\\\\Domain\\\\FilePathCandidates\\.$#"
135-
count: 1
136-
path: lib/Domain/ClassToFileFileToClass.php
137-
138-
-
139-
message: "#^Method Phpactor\\\\ClassFileConverter\\\\Domain\\\\ClassToFileFileToClass\\:\\:fileToClassCandidates\\(\\) return type has no value type specified in iterable type Phpactor\\\\ClassFileConverter\\\\Domain\\\\ClassNameCandidates\\.$#"
140-
count: 1
141-
path: lib/Domain/ClassToFileFileToClass.php
142-
14398
-
14499
message: "#^Method Phpactor\\\\ClassFileConverter\\\\Domain\\\\FilePath\\:\\:fromParts\\(\\) has parameter \\$parts with no value type specified in iterable type array\\.$#"
145100
count: 1
146101
path: lib/Domain/FilePath.php
147102

148103
-
149-
message: "#^Method Phpactor\\\\ClassFileConverter\\\\Domain\\\\FilePath\\:\\:fromString\\(\\) has parameter \\$path with no typehint specified\\.$#"
104+
message: "#^Method Phpactor\\\\ClassFileConverter\\\\Domain\\\\FilePath\\:\\:fromString\\(\\) has parameter \\$path with no type specified\\.$#"
150105
count: 1
151106
path: lib/Domain/FilePath.php
152107

@@ -161,12 +116,12 @@ parameters:
161116
path: lib/Domain/FilePathCandidates.php
162117

163118
-
164-
message: "#^Method Phpactor\\\\ClassFileConverter\\\\Domain\\\\FilePathCandidates\\:\\:create\\(\\) has no return typehint specified\\.$#"
119+
message: "#^Method Phpactor\\\\ClassFileConverter\\\\Domain\\\\FilePathCandidates\\:\\:create\\(\\) has no return type specified\\.$#"
165120
count: 1
166121
path: lib/Domain/FilePathCandidates.php
167122

168123
-
169-
message: "#^Method Phpactor\\\\ClassFileConverter\\\\Domain\\\\FilePathCandidates\\:\\:fromFilePaths\\(\\) has no return typehint specified\\.$#"
124+
message: "#^Method Phpactor\\\\ClassFileConverter\\\\Domain\\\\FilePathCandidates\\:\\:fromFilePaths\\(\\) has no return type specified\\.$#"
170125
count: 1
171126
path: lib/Domain/FilePathCandidates.php
172127

@@ -175,23 +130,12 @@ parameters:
175130
count: 1
176131
path: lib/Domain/FilePathCandidates.php
177132

178-
-
179-
message: "#^Method Phpactor\\\\ClassFileConverter\\\\Domain\\\\FilePathCandidates\\:\\:getIterator\\(\\) return type has no value type specified in iterable type Traversable\\<mixed, mixed\\>\\.$#"
180-
count: 1
181-
path: lib/Domain/FilePathCandidates.php
182-
183133
-
184134
message: "#^Method Phpactor\\\\ClassFileConverter\\\\Domain\\\\FilePathCandidates\\:\\:toArray\\(\\) return type has no value type specified in iterable type array\\.$#"
185135
count: 1
186136
path: lib/Domain/FilePathCandidates.php
187137

188138
-
189-
message: "#^Property Phpactor\\\\ClassFileConverter\\\\Domain\\\\FilePathCandidates\\:\\:\\$filePaths has no typehint specified\\.$#"
139+
message: "#^Property Phpactor\\\\ClassFileConverter\\\\Domain\\\\FilePathCandidates\\:\\:\\$filePaths has no type specified\\.$#"
190140
count: 1
191141
path: lib/Domain/FilePathCandidates.php
192-
193-
-
194-
message: "#^Method Phpactor\\\\ClassFileConverter\\\\Domain\\\\FileToClass\\:\\:fileToClassCandidates\\(\\) return type has no value type specified in iterable type Phpactor\\\\ClassFileConverter\\\\Domain\\\\ClassNameCandidates\\.$#"
195-
count: 1
196-
path: lib/Domain/FileToClass.php
197-

tests/Integration/Simple/SimpleClassToFileTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ public function testClassToFile(): void
3030
]), $candidates);
3131
}
3232

33+
public function testClassToFileInvalid(): void
34+
{
35+
$candidates = $this->classToFile->classToFileCandidates(
36+
ClassName::fromString('InvalidClass')
37+
);
38+
39+
$this->assertEquals(FilePathCandidates::fromFilePaths([
40+
]), $candidates);
41+
}
42+
3343
public function testClassToFileDeeper(): void
3444
{
3545
$candidates = $this->classToFile->classToFileCandidates(ClassName::fromString('Acme\\NamespaceHere\\Hallo'));
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Phpactor\ClassFileConverter\Tests\Integration\Simple\project\lib;
4+
5+
class
6+
{
7+
}

0 commit comments

Comments
 (0)