File tree Expand file tree Collapse file tree 6 files changed +37
-5
lines changed Expand file tree Collapse file tree 6 files changed +37
-5
lines changed Original file line number Diff line number Diff line change 1919 "phpstan/phpstan" : " ~0.12.0" ,
2020 "phpunit/phpunit" : " ^9.0" ,
2121 "symfony/filesystem" : " ^4.2 || ^5.0" ,
22- "phpspec/prophecy-phpunit" : " ^2.0"
22+ "phpspec/prophecy-phpunit" : " ^2.0" ,
23+ "symfony/var-dumper" : " ^6.0"
2324 },
2425 "extra" : {
2526 "branch-alias" : {
Original file line number Diff line number Diff line change 33namespace Phpactor \ClassFileConverter \Adapter \Simple ;
44
55use RuntimeException ;
6+ use function token_name ;
67
78/**
89 * Return the class name from a file.
@@ -13,6 +14,10 @@ class ClassScanner
1314{
1415 public function getClassNameFromFile (string $ file ): ?string
1516 {
17+ if (!file_exists ($ file )) {
18+ return null ;
19+ }
20+
1621 $ fp = fopen ($ file , 'r ' );
1722
1823 $ class = $ namespace = $ buffer = '' ;
@@ -47,7 +52,7 @@ public function getClassNameFromFile(string $file): ?string
4752 }
4853 }
4954
50- if ($ tokens [$ i ][ 0 ] === T_CLASS ) {
55+ if ($ this -> isClassLike ( $ tokens [$ i ]) ) {
5156 for ($ j = $ i + 1 ; $ j < count ($ tokens ); $ j ++) {
5257 if ($ tokens [$ j ][0 ] === T_STRING ) {
5358 $ class = $ tokens [$ i + 2 ][1 ];
@@ -63,6 +68,22 @@ public function getClassNameFromFile(string $file): ?string
6368 return null ;
6469 }
6570
66- return $ namespace . '\\' . $ class ;
71+ return ltrim ($ namespace . '\\' . $ class , '\\' );
72+ }
73+
74+ private function isClassLike ($ token ): bool
75+ {
76+ if (!is_array ($ token )) {
77+ return false ;
78+ }
79+ if ($ token [0 ] === T_CLASS || $ token [0 ] === T_INTERFACE || $ token [0 ] === T_TRAIT ) {
80+ return true ;
81+ }
82+
83+ if (!defined ('T_ENUM ' )) {
84+ return false ;
85+ }
86+
87+ return $ token [0 ] === T_ENUM ;
6788 }
6889}
Original file line number Diff line number Diff line change @@ -41,8 +41,9 @@ public function classToFileCandidates(ClassName $className): FilePathCandidates
4141 $ iterator = new RegexIterator ($ iterator , $ pattern );
4242
4343 foreach ($ iterator as $ phpFile ) {
44- $ className = $ this ->classScanner ->getClassNameFromFile ($ phpFile ->getPathName ());
45- if (ClassName::fromString ($ className ) == $ className ) {
44+ $ scannedClass = $ this ->classScanner ->getClassNameFromFile ($ phpFile ->getPathName ());
45+
46+ if (ClassName::fromString ($ scannedClass ) == $ className ) {
4647 $ candidates [] = FilePath::fromString ($ phpFile ->getPathName ());
4748 }
4849 }
Original file line number Diff line number Diff line change 88use Phpactor \ClassFileConverter \Domain \ClassName ;
99use Phpactor \ClassFileConverter \Domain \FilePathCandidates ;
1010use Phpactor \ClassFileConverter \Domain \FilePath ;
11+ use Prophecy \PhpUnit \ProphecyTrait ;
1112
1213class ChainClassToFileTest extends TestCase
1314{
15+ use ProphecyTrait;
16+
1417 private $ classToFile1 ;
1518 private $ classToFile2 ;
1619
Original file line number Diff line number Diff line change 88use Phpactor \ClassFileConverter \Domain \ClassNameCandidates ;
99use Phpactor \ClassFileConverter \Domain \ClassName ;
1010use Phpactor \ClassFileConverter \Domain \FilePath ;
11+ use Prophecy \PhpUnit \ProphecyTrait ;
1112
1213class ChainFileToClassTest extends TestCase
1314{
15+ use ProphecyTrait;
16+
1417 private $ fileToClass1 ;
1518 private $ fileToClass2 ;
1619
Original file line number Diff line number Diff line change 1010use Phpactor \ClassFileConverter \Domain \FilePathCandidates ;
1111use Phpactor \ClassFileConverter \Domain \ClassNameCandidates ;
1212use Phpactor \ClassFileConverter \Domain \FilePath ;
13+ use Prophecy \PhpUnit \ProphecyTrait ;
1314
1415class CompositeTransformerTest extends TestCase
1516{
17+ use ProphecyTrait;
18+
1619 private $ transformer ;
1720
1821 public function setUp (): void
You can’t perform that action at this time.
0 commit comments