66use SensioLabs \DeprecationDetector \FileInfo \PhpFileInfo ;
77use SensioLabs \DeprecationDetector \Parser \ParserInterface ;
88use Symfony \Component \Finder \Finder ;
9+ use PhpParser \Error ;
910
10- class ParsedPhpFileFinder extends Finder
11+ class ParsedPhpFileFinder
1112{
1213 /**
1314 * @var ParserInterface
1415 */
15- protected $ parser ;
16+ private $ parser ;
1617
1718 /**
1819 * @var VerboseProgressOutput
1920 */
20- protected $ progressOutput ;
21+ private $ progressOutput ;
2122
2223 /**
23- * @var \PhpParser\Error[]
24+ * @var FinderFactoryInterface
2425 */
25- protected $ parserErrors = array () ;
26+ private $ finderFactory ;
2627
2728 /**
28- * @param ParserInterface $parser
29- * @param VerboseProgressOutput $progressOutput
29+ * @param ParserInterface $parser
30+ * @param VerboseProgressOutput $progressOutput
31+ * @param FinderFactoryInterface $finderFactory
3032 */
31- public function __construct (ParserInterface $ parser , VerboseProgressOutput $ progressOutput )
33+ public function __construct (ParserInterface $ parser , VerboseProgressOutput $ progressOutput, FinderFactoryInterface $ finderFactory )
3234 {
33- parent ::__construct ();
34-
3535 $ this ->parser = $ parser ;
36- $ this ->files ()->name ('*.php ' );
37-
3836 $ this ->progressOutput = $ progressOutput ;
37+ $ this ->finderFactory = $ finderFactory ;
3938 }
4039
4140 /**
42- * @return \Iterator
41+ * @param string $path
42+ * @return Result
4343 */
44- public function getIterator ( )
44+ public function parsePhpFiles ( $ path )
4545 {
46- $ iterator = parent :: getIterator ( );
47- $ files = new \ ArrayIterator ();
48- $ total = $ this -> count ();
46+ $ files = $ this -> finderFactory -> createFinder ()-> in ( $ path );
47+ $ parsedFiles = array ();
48+ $ parserErrors = array ();
4949
50- $ this ->progressOutput ->start ($ total );
50+ $ this ->progressOutput ->start ($ fileCount = $ files -> count () );
5151
5252 $ i = 0 ;
53- foreach ($ iterator as $ file ) {
53+ foreach ($ files -> getIterator () as $ file ) {
5454 $ file = PhpFileInfo::create ($ file );
5555
5656 try {
5757 $ this ->progressOutput ->advance (++$ i , $ file );
5858 $ this ->parser ->parseFile ($ file );
59- } catch (\ PhpParser \ Error $ ex ) {
59+ } catch (Error $ ex ) {
6060 $ raw = $ ex ->getRawMessage ().' in file ' .$ file ;
6161 $ ex ->setRawMessage ($ raw );
62- $ this -> parserErrors [] = $ ex ;
62+ $ parserErrors [] = $ ex ;
6363 }
6464
65- $ files -> append ( $ file) ;
65+ $ parsedFiles [] = $ file ;
6666 }
6767
6868 $ this ->progressOutput ->end ();
69-
70- return $ files ;
71- }
72-
73- /**
74- * @return int
75- */
76- public function count ()
77- {
78- return iterator_count (parent ::getIterator ());
79- }
80-
81- /**
82- * @return \PhpParser\Error[]
83- */
84- public function getParserErrors ()
85- {
86- return $ this ->parserErrors ;
87- }
88-
89- /**
90- * @param ParserInterface $parser
91- * @param VerboseProgressOutput $progressOutput
92- * @return ParsedPhpFileFinder
93- */
94- public static function deprecationFinder (ParserInterface $ parser , VerboseProgressOutput $ progressOutput )
95- {
96- $ finder = new ParsedPhpFileFinder ($ parser , $ progressOutput );
97- $ finder
98- ->contains ('@deprecated ' )
99- ->exclude ('vendor ' )
100- ->exclude ('Tests ' )
101- ->exclude ('Test ' );
102-
103- return $ finder ;
104-
105- }
106-
107- /**
108- * @param ParserInterface $parser
109- * @param VerboseProgressOutput $progressOutput
110- * @return ParsedPhpFileFinder
111- */
112- public static function usageFinder (ParserInterface $ parser , VerboseProgressOutput $ progressOutput )
113- {
114- $ finder = new ParsedPhpFileFinder ($ parser , $ progressOutput );
115- $ finder
116- ->exclude ('vendor ' )
117- ->exclude ('Tests ' )
118- ->exclude ('Test ' );
119-
120- return $ finder ;
121- }
122-
123- /**
124- * @return ParserInterface
125- */
126- public function getParser ()
127- {
128- return $ this ->parser ;
129- }
130-
131- /**
132- * @return VerboseProgressOutput
133- */
134- public function getOutput ()
135- {
136- return $ this ->progressOutput ;
69+ return new Result ($ parsedFiles , $ parserErrors , $ fileCount );
13770 }
138- }
71+ }
0 commit comments