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 TranslatableExceptionsFileVisitor 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 $exceptionsToExtractFrom = 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 | */ |
||
104 | public function enterNode(Node $node) |
||
105 | { |
||
106 | if (!$node instanceof Node\Stmt\Throw_ |
||
|
|||
107 | || !$node->expr instanceof Node\Expr\New_) { |
||
108 | $this->previousNode = $node; |
||
109 | |||
110 | return; |
||
111 | } |
||
112 | |||
113 | $exceptionClass = $node->expr->class->parts[0]; |
||
114 | if (!in_array(strtolower($exceptionClass), array_map('strtolower', $this->exceptionsToExtractFrom))) { |
||
115 | $this->previousNode = $node; |
||
116 | |||
117 | return; |
||
118 | } |
||
119 | |||
120 | $node = $node->expr; |
||
121 | $ignore = false; |
||
122 | $desc = $meaning = null; |
||
123 | View Code Duplication | if (null !== $docComment = $this->getDocCommentForNode($node)) { |
|
124 | if ($docComment instanceof Doc) { |
||
125 | $docComment = $docComment->getText(); |
||
126 | } |
||
127 | foreach ($this->docParser->parse($docComment, 'file ' . $this->file . ' near line ' . $node->getLine()) as $annot) { |
||
128 | if ($annot instanceof Ignore) { |
||
129 | $ignore = true; |
||
130 | } elseif ($annot instanceof Desc) { |
||
131 | $desc = $annot->text; |
||
132 | } elseif ($annot instanceof Meaning) { |
||
133 | $meaning = $annot->text; |
||
134 | } |
||
135 | } |
||
136 | } |
||
137 | |||
138 | View Code Duplication | if (!$node->args[0]->value instanceof String_) { |
|
139 | if ($ignore) { |
||
140 | return; |
||
141 | } |
||
142 | |||
143 | $message = sprintf('Can only extract the translation id from a scalar string, but got "%s". Please refactor your code to make it extractable, or add the doc comment /** @Ignore */ to this code element (in %s on line %d).', get_class($node->args[0]->value), $this->file, $node->args[0]->value->getLine()); |
||
144 | |||
145 | if ($this->logger) { |
||
146 | $this->logger->error($message); |
||
147 | |||
148 | return; |
||
149 | } |
||
150 | |||
151 | throw new RuntimeException($message); |
||
152 | } |
||
153 | |||
154 | $id = $node->args[0]->value->value; |
||
155 | |||
156 | $message = new Message($id, $this->defaultDomain); |
||
157 | $message->setDesc($desc); |
||
158 | $message->setMeaning($meaning); |
||
159 | $message->addSource($this->fileSourceFactory->create($this->file, $node->getLine())); |
||
160 | $this->catalogue->add($message); |
||
161 | } |
||
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) |
||
169 | { |
||
170 | $this->file = $file; |
||
171 | $this->catalogue = $catalogue; |
||
172 | $this->traverser->traverse($ast); |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * @param array $nodes |
||
177 | */ |
||
178 | public function beforeTraverse(array $nodes) |
||
181 | |||
182 | /** |
||
183 | * @param Node $node |
||
184 | */ |
||
185 | public function leaveNode(Node $node) |
||
188 | |||
189 | /** |
||
190 | * @param array $nodes |
||
191 | */ |
||
192 | public function afterTraverse(array $nodes) |
||
195 | |||
196 | /** |
||
197 | * @param \SplFileInfo $file |
||
198 | * @param MessageCatalogue $catalogue |
||
199 | */ |
||
200 | public function visitFile(\SplFileInfo $file, MessageCatalogue $catalogue) |
||
203 | |||
204 | /** |
||
205 | * @param \SplFileInfo $file |
||
206 | * @param MessageCatalogue $catalogue |
||
207 | * @param \Twig_Node $ast |
||
208 | */ |
||
209 | public function visitTwigFile(\SplFileInfo $file, MessageCatalogue $catalogue, \Twig_Node $ast) |
||
212 | |||
213 | /** |
||
214 | * @param Node $node |
||
215 | * @return null|string |
||
216 | */ |
||
217 | View Code Duplication | private function getDocCommentForNode(Node $node) |
|
239 | } |
||
240 |
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.