Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
28 | class ValidationErrorFileVisitor implements LoggerAwareInterface, FileVisitorInterface, NodeVisitor |
||
29 | { |
||
30 | /** |
||
31 | * @var FileSourceFactory |
||
32 | */ |
||
33 | private $fileSourceFactory; |
||
34 | |||
35 | /** |
||
36 | * @var NodeTraverser |
||
37 | */ |
||
38 | private $traverser; |
||
39 | |||
40 | /** |
||
41 | * @var MessageCatalogue |
||
42 | */ |
||
43 | private $catalogue; |
||
44 | |||
45 | /** |
||
46 | * @var \SplFileInfo |
||
47 | */ |
||
48 | private $file; |
||
49 | |||
50 | /** |
||
51 | * @var DocParser |
||
52 | */ |
||
53 | private $docParser; |
||
54 | |||
55 | /** |
||
56 | * @var LoggerInterface |
||
57 | */ |
||
58 | private $logger; |
||
59 | |||
60 | /** |
||
61 | * @var Node |
||
62 | */ |
||
63 | private $previousNode; |
||
64 | |||
65 | /** |
||
66 | * @var string |
||
67 | */ |
||
68 | protected $defaultDomain = 'repository_exceptions'; |
||
69 | |||
70 | /** |
||
71 | * Methods and "domain" parameter offset to extract from PHP code |
||
72 | * |
||
73 | * @var array method => position of the "domain" parameter |
||
74 | */ |
||
75 | protected $classToExtractFrom = array( |
||
76 | 'contentvalidationexception', |
||
77 | 'forbiddenexception' |
||
78 | ); |
||
79 | |||
80 | /** |
||
81 | * DefaultPhpFileExtractor constructor. |
||
82 | * @param DocParser $docParser |
||
83 | * @param FileSourceFactory $fileSourceFactory |
||
84 | */ |
||
85 | View Code Duplication | public function __construct(DocParser $docParser, FileSourceFactory $fileSourceFactory) |
|
92 | |||
93 | /** |
||
94 | * @param LoggerInterface $logger |
||
95 | */ |
||
96 | public function setLogger(LoggerInterface $logger) |
||
100 | |||
101 | /** |
||
102 | * @param Node $node |
||
103 | * @return void |
||
104 | */ |
||
105 | public function enterNode(Node $node) |
||
162 | |||
163 | /** |
||
164 | * @param \SplFileInfo $file |
||
165 | * @param MessageCatalogue $catalogue |
||
166 | * @param array $ast |
||
167 | */ |
||
168 | public function visitPhpFile(\SplFileInfo $file, MessageCatalogue $catalogue, array $ast) |
||
174 | |||
175 | /** |
||
176 | * @param array $nodes |
||
177 | * @return void |
||
178 | */ |
||
179 | public function beforeTraverse(array $nodes) |
||
182 | |||
183 | /** |
||
184 | * @param Node $node |
||
185 | * @return void |
||
186 | */ |
||
187 | public function leaveNode(Node $node) |
||
190 | |||
191 | /** |
||
192 | * @param array $nodes |
||
193 | * @return void |
||
194 | */ |
||
195 | public function afterTraverse(array $nodes) |
||
198 | |||
199 | /** |
||
200 | * @param \SplFileInfo $file |
||
201 | * @param MessageCatalogue $catalogue |
||
202 | */ |
||
203 | public function visitFile(\SplFileInfo $file, MessageCatalogue $catalogue) |
||
206 | |||
207 | /** |
||
208 | * @param \SplFileInfo $file |
||
209 | * @param MessageCatalogue $catalogue |
||
210 | * @param \Twig_Node $ast |
||
211 | */ |
||
212 | public function visitTwigFile(\SplFileInfo $file, MessageCatalogue $catalogue, \Twig_Node $ast) |
||
215 | |||
216 | /** |
||
217 | * @param Node $node |
||
218 | * @return null|string |
||
219 | */ |
||
220 | View Code Duplication | private function getDocCommentForNode(Node $node) |
|
241 | } |
||
242 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.