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 |
||
| 18 | class RqlFieldsCompilerPass implements CompilerPassInterface |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @var DocumentMap |
||
| 22 | */ |
||
| 23 | private $documentMap; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Make extref fields map and set it to parameter |
||
| 27 | * |
||
| 28 | * @param ContainerBuilder $container container builder |
||
| 29 | * @return void |
||
| 30 | */ |
||
| 31 | 4 | public function process(ContainerBuilder $container) |
|
| 32 | { |
||
| 33 | 4 | $this->documentMap = $container->get('graviton.document.map'); |
|
| 34 | |||
| 35 | 4 | $map = []; |
|
| 36 | |||
| 37 | 4 | $services = array_keys($container->findTaggedServiceIds('graviton.rest')); |
|
| 38 | 4 | foreach ($services as $id) { |
|
| 39 | 4 | list($ns, $bundle, , $doc) = explode('.', $id); |
|
| 40 | 4 | if (empty($bundle) || empty($doc)) { |
|
| 41 | continue; |
||
| 42 | } |
||
| 43 | |||
| 44 | 4 | $className = $this->getServiceDocument( |
|
| 45 | 4 | $container->getDefinition($id), |
|
| 46 | 4 | $ns, |
|
| 47 | 4 | $bundle, |
|
| 48 | 2 | $doc |
|
| 49 | 2 | ); |
|
| 50 | 4 | $rqlFields = $this->documentMap->getFieldNamesFlat($this->documentMap->getDocument($className)); |
|
| 51 | 4 | $routePrefix = strtolower($ns.'.'.$bundle.'.'.'rest'.'.'.$doc); |
|
| 52 | |||
| 53 | 4 | $map[$routePrefix.'.get'] = $rqlFields; |
|
| 54 | 4 | $map[$routePrefix.'.all'] = $rqlFields; |
|
| 55 | 2 | } |
|
| 56 | |||
| 57 | 4 | $container->setParameter('graviton.document.rql.fields', $map); |
|
| 58 | 4 | } |
|
| 59 | |||
| 60 | /** |
||
| 61 | * Get document class name from service |
||
| 62 | * |
||
| 63 | * @param Definition $service Service definition |
||
| 64 | * @param string $ns Bundle namespace |
||
| 65 | * @param string $bundle Bundle name |
||
| 66 | * @param string $doc Document name |
||
| 67 | * @return string |
||
| 68 | */ |
||
| 69 | 4 | View Code Duplication | private function getServiceDocument(Definition $service, $ns, $bundle, $doc) |
| 88 | } |
||
| 89 |