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 | */ |
||
| 104 | public function enterNode(Node $node) |
||
| 105 | { |
||
| 106 | if (!$node instanceof Node\Expr\New_ |
||
| 107 | || !is_string($node->class) |
||
| 108 | || strtolower($node->class) !== 'validationerror') { |
||
| 109 | $this->previousNode = $node; |
||
| 110 | |||
| 111 | return; |
||
| 112 | } |
||
| 113 | |||
| 114 | $ignore = false; |
||
| 115 | $desc = $meaning = null; |
||
| 116 | View Code Duplication | if (null !== $docComment = $this->getDocCommentForNode($node)) { |
|
|
|
|||
| 117 | if ($docComment instanceof Doc) { |
||
| 118 | $docComment = $docComment->getText(); |
||
| 119 | } |
||
| 120 | foreach ($this->docParser->parse($docComment, 'file ' . $this->file . ' near line ' . $node->getLine()) as $annot) { |
||
| 121 | if ($annot instanceof Ignore) { |
||
| 122 | $ignore = true; |
||
| 123 | } elseif ($annot instanceof Desc) { |
||
| 124 | $desc = $annot->text; |
||
| 125 | } elseif ($annot instanceof Meaning) { |
||
| 126 | $meaning = $annot->text; |
||
| 127 | } |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | View Code Duplication | if (!$node->args[0]->value instanceof String_) { |
|
| 132 | if ($ignore) { |
||
| 133 | return; |
||
| 134 | } |
||
| 135 | |||
| 136 | $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()); |
||
| 137 | |||
| 138 | if ($this->logger) { |
||
| 139 | $this->logger->error($message); |
||
| 140 | |||
| 141 | return; |
||
| 142 | } |
||
| 143 | |||
| 144 | throw new RuntimeException($message); |
||
| 145 | } |
||
| 146 | |||
| 147 | $message = new Message($node->args[0]->value->value, $this->defaultDomain); |
||
| 148 | $message->setDesc($desc); |
||
| 149 | $message->setMeaning($meaning); |
||
| 150 | $message->addSource($this->fileSourceFactory->create($this->file, $node->getLine())); |
||
| 151 | $this->catalogue->add($message); |
||
| 152 | |||
| 153 | // plural |
||
| 154 | if ($node->args[1]->value instanceof String_) { |
||
| 155 | $message = new Message($node->args[1]->value->value, $this->defaultDomain); |
||
| 156 | $message->setDesc($desc); |
||
| 157 | $message->setMeaning($meaning); |
||
| 158 | $message->addSource($this->fileSourceFactory->create($this->file, $node->getLine())); |
||
| 159 | $this->catalogue->add($message); |
||
| 160 | } |
||
| 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) |
||
| 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 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.