11<?php
22
3- namespace Symfony \CodeBlockChecker \Service ;
3+ namespace Symfony \CodeBlockChecker \Service \ CodeRunner ;
44
55use Doctrine \RST \Nodes \CodeNode ;
66use Symfony \CodeBlockChecker \Issue \Issue ;
@@ -18,21 +18,17 @@ class CodeNodeRunner
1818 /**
1919 * @param list<CodeNode> $nodes
2020 */
21- public function runNodes (array $ nodes , string $ applicationDirectory ): IssueCollection
21+ public function run (array $ nodes , IssueCollection $ issues , string $ applicationDirectory ): void
2222 {
23- $ issues = new IssueCollection ();
2423 foreach ($ nodes as $ node ) {
2524 $ this ->processNode ($ node , $ issues , $ applicationDirectory );
2625 }
27-
28- return $ issues ;
2926 }
3027
3128 private function processNode (CodeNode $ node , IssueCollection $ issues , string $ applicationDirectory ): void
3229 {
3330 $ explodedNode = explode ("\n" , $ node ->getValue ());
3431 $ file = $ this ->getFile ($ node , $ explodedNode );
35- $ this ->verifyPhpClasses ($ node , $ issues , $ explodedNode , $ applicationDirectory );
3632
3733 if ('config/packages/ ' !== substr ($ file , 0 , 16 )) {
3834 return ;
@@ -105,83 +101,4 @@ private function getNodeContents(CodeNode $node, array $contents): string
105101
106102 return implode ("\n" , $ contents );
107103 }
108-
109- /**
110- * Make sure PHP classes exists in the application directory.
111- *
112- * @param list<string> $codeBlock
113- */
114- private function verifyPhpClasses (CodeNode $ node , IssueCollection $ issues , array $ codeBlock , string $ applicationDirectory ): void
115- {
116- $ language = $ node ->getLanguage () ?? 'php ' ;
117- if (!in_array ($ language , ['php ' , 'php-symfony ' , 'php-standalone ' , 'php-annotations ' ])) {
118- return ;
119- }
120-
121- $ fileBody = '' ;
122- $ classes = [];
123- foreach ($ codeBlock as $ i => $ line ) {
124- $ matches = [];
125- if (0 !== strpos ($ line , 'use ' ) || !preg_match ('|^use (.* \\\.*); *?$|m ' , $ line , $ matches )) {
126- continue ;
127- }
128-
129- $ class = $ matches [1 ];
130- if (false !== $ pos = strpos ($ class , ' as ' )) {
131- $ class = substr ($ class , 0 , $ pos );
132- }
133-
134- if (false !== $ pos = strpos ($ class , 'function ' )) {
135- continue ;
136- }
137-
138- $ explode = explode ('\\' , $ class );
139- if (
140- 'App ' === $ explode [0 ] || 'Acme ' === $ explode [0 ]
141- || (3 === count ($ explode ) && 'Symfony ' === $ explode [0 ] && ('Component ' === $ explode [1 ] || 'Config ' === $ explode [1 ]))
142- ) {
143- continue ;
144- }
145-
146- $ classes [$ i ] = ['class ' => $ class , 'line ' => $ i + 1 ];
147- $ fileBody .= sprintf ('%s => isLoaded("%s"), ' , $ i , $ class )."\n" ;
148- }
149-
150- if ([] === $ classes ) {
151- return ;
152- }
153-
154- file_put_contents ($ applicationDirectory .'/class_exist.php ' , strtr ('<?php
155- require __DIR__. \'/vendor/autoload.php \';
156-
157- function isLoaded($class) {
158- return class_exists($class) || interface_exists($class) || trait_exists($class);
159- }
160-
161- echo json_encode([ARRAY_CONTENT]);
162-
163- ' , ['ARRAY_CONTENT ' => $ fileBody ]));
164-
165- $ process = new Process (['php ' , 'class_exist.php ' ], $ applicationDirectory );
166- $ process ->run ();
167-
168- if (!$ process ->isSuccessful ()) {
169- // TODO handle this
170- return ;
171- }
172-
173- $ output = $ process ->getOutput ();
174- try {
175- $ results = json_decode ($ output , true , 512 , JSON_THROW_ON_ERROR );
176- } catch (\JsonException $ e ) {
177- // TODO handle this
178- return ;
179- }
180-
181- foreach ($ classes as $ i => $ classData ) {
182- if (!$ results [$ i ]) {
183- $ issues ->addIssue (new Issue ($ node , sprintf ('Class, interface or trait with name "%s" does not exist ' , $ classData ['class ' ]), 'Missing class ' , $ node ->getEnvironment ()->getCurrentFileName (), $ classData ['line ' ]));
184- }
185- }
186- }
187104}
0 commit comments