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 |
||
24 | final class SourceLocationContainerVisitor extends BasePHPVisitor implements NodeVisitor |
||
25 | { |
||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | private $namespace = ''; |
||
30 | |||
31 | /** |
||
32 | * @var array |
||
33 | */ |
||
34 | private $useStatements = []; |
||
35 | |||
36 | 1 | public function beforeTraverse(array $nodes) |
|
39 | |||
40 | 1 | public function enterNode(Node $node) |
|
41 | { |
||
42 | 1 | View Code Duplication | if ($node instanceof Node\Stmt\Namespace_) { |
|
|||
43 | 1 | if (isset($node->name)) { |
|
44 | // Save namespace of this class for later. |
||
45 | 1 | $this->namespace = implode('\\', $node->name->parts); |
|
46 | } |
||
47 | 1 | $this->useStatements = []; |
|
48 | |||
49 | 1 | return; |
|
50 | } |
||
51 | |||
52 | 1 | if ($node instanceof Node\Stmt\UseUse) { |
|
53 | 1 | $this->useStatements[$node->alias] = implode('\\', $node->name->parts); |
|
54 | |||
55 | 1 | return; |
|
56 | } |
||
57 | |||
58 | 1 | if (!$node instanceof Node\Stmt\Class_) { |
|
59 | 1 | return; |
|
60 | } |
||
61 | |||
62 | 1 | $isContainer = false; |
|
63 | 1 | foreach ($node->implements as $interface) { |
|
64 | 1 | $name = implode('\\', $interface->parts); |
|
65 | 1 | if (isset($this->useStatements[$name])) { |
|
66 | 1 | $name = $this->useStatements[$name]; |
|
67 | } |
||
68 | |||
69 | 1 | if ('Translation\Extractor\TranslationSourceLocationContainer' === $name) { |
|
70 | 1 | $isContainer = true; |
|
71 | |||
72 | 1 | break; |
|
73 | } |
||
74 | } |
||
75 | |||
76 | 1 | if (!$isContainer) { |
|
77 | return; |
||
78 | } |
||
79 | |||
80 | 1 | $sourceLocations = call_user_func([$this->namespace.'\\'.$node->name, 'getTranslationSourceLocations']); |
|
81 | 1 | if (!is_array($sourceLocations)) { |
|
82 | throw new \RuntimeException(sprintf('%s::getTranslationSourceLocations() was expected to return an array of SourceLocations, but got %s.', $this->namespace.'\\'.$node->name, gettype($sourceLocations))); |
||
83 | } |
||
84 | |||
85 | 1 | foreach ($sourceLocations as $sourceLocation) { |
|
86 | 1 | if (!$sourceLocation instanceof SourceLocation) { |
|
87 | throw new \RuntimeException(sprintf('%s::getTranslationSourceLocations() was expected to return an array of SourceLocations, but got an array which contains an item of type %s.', $this->namespace.'\\'.$node->name, gettype($sourceLocation))); |
||
88 | } |
||
89 | |||
90 | 1 | $this->collection->addLocation($sourceLocation); |
|
91 | } |
||
92 | 1 | } |
|
93 | |||
94 | 1 | public function leaveNode(Node $node) |
|
97 | |||
98 | 1 | public function afterTraverse(array $nodes) |
|
101 | } |
||
102 |
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.