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 |
||
| 40 | class OptimizedJsonRepository extends AbstractJsonRepository implements EditableRepository |
||
| 41 | { |
||
| 42 | /** |
||
| 43 | * {@inheritdoc} |
||
| 44 | */ |
||
| 45 | 76 | protected function insertReference($path, $reference) |
|
| 49 | |||
| 50 | /** |
||
| 51 | * {@inheritdoc} |
||
| 52 | */ |
||
| 53 | 7 | protected function removeReferences($glob) |
|
| 65 | |||
| 66 | /** |
||
| 67 | * {@inheritdoc} |
||
| 68 | */ |
||
| 69 | 52 | protected function getReferencesForPath($path) |
|
| 94 | |||
| 95 | /** |
||
| 96 | * {@inheritdoc} |
||
| 97 | */ |
||
| 98 | 22 | View Code Duplication | protected function getReferencesForGlob($glob, $flags = 0) |
| 99 | { |
||
| 100 | 22 | if (!Glob::isDynamic($glob)) { |
|
| 101 | 16 | return $this->getReferencesForPath($glob); |
|
| 102 | } |
||
| 103 | |||
| 104 | 13 | return $this->getReferencesForRegex( |
|
| 105 | 13 | Glob::getStaticPrefix($glob), |
|
| 106 | 13 | Glob::toRegEx($glob), |
|
| 107 | $flags |
||
| 108 | 13 | ); |
|
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * {@inheritdoc} |
||
| 113 | */ |
||
| 114 | 38 | protected function getReferencesForRegex($staticPrefix, $regex, $flags = 0) |
|
| 115 | 38 | { |
|
| 116 | 25 | $result = array(); |
|
| 117 | 25 | $foundMappingsWithPrefix = false; |
|
| 118 | |||
| 119 | 25 | foreach ($this->json as $path => $reference) { |
|
| 120 | 25 | if (0 === strpos($path, $staticPrefix)) { |
|
| 121 | 21 | $foundMappingsWithPrefix = true; |
|
| 122 | |||
| 123 | 21 | if (preg_match($regex, $path)) { |
|
| 124 | // We're only interested in the first entry of eventual arrays |
||
| 125 | 20 | if (is_array($reference)) { |
|
| 126 | $reference = reset($reference); |
||
| 127 | } |
||
| 128 | |||
| 129 | 20 | View Code Duplication | if ($this->isFilesystemReference($reference)) { |
| 130 | 20 | $reference = Path::makeAbsolute($reference, $this->baseDirectory); |
|
| 131 | |||
| 132 | // Ignore non-existing files. Not sure this is the right |
||
| 133 | // thing to do. |
||
| 134 | 20 | if (!file_exists($reference)) { |
|
| 135 | continue; |
||
| 136 | } |
||
| 137 | 20 | } |
|
| 138 | |||
| 139 | 20 | $result[$path] = $reference; |
|
| 140 | |||
| 141 | 20 | if ($flags & self::STOP_ON_FIRST) { |
|
| 142 | 4 | return $result; |
|
| 143 | } |
||
| 144 | 17 | } |
|
| 145 | |||
| 146 | 19 | continue; |
|
| 147 | } |
||
| 148 | |||
| 149 | // We did not find anything but previously found mappings with the |
||
| 150 | // static prefix |
||
| 151 | // The mappings are sorted alphabetically, so we can safely abort |
||
| 152 | 21 | if ($foundMappingsWithPrefix) { |
|
| 153 | 15 | break; |
|
| 154 | } |
||
| 155 | 24 | } |
|
| 156 | |||
| 157 | 24 | return $result; |
|
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * {@inheritdoc} |
||
| 162 | */ |
||
| 163 | 11 | View Code Duplication | protected function getReferencesInDirectory($path, $flags = 0) |
| 164 | { |
||
| 165 | 11 | $basePath = rtrim($path, '/'); |
|
| 166 | |||
| 167 | 11 | return $this->getReferencesForRegex( |
|
| 168 | 11 | $basePath.'/', |
|
| 169 | 11 | '~^'.preg_quote($basePath, '~').'/[^/]+$~', |
|
| 170 | $flags |
||
| 171 | 11 | ); |
|
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * {@inheritdoc} |
||
| 176 | */ |
||
| 177 | 76 | protected function addFilesystemResource($path, FilesystemResource $resource) |
|
| 191 | } |
||
| 192 |
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.