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 |
||
27 | class ExtRefFieldsCompilerPass implements CompilerPassInterface |
||
28 | { |
||
29 | |||
30 | /** |
||
31 | * @var DocumentMap |
||
32 | */ |
||
33 | private $documentMap; |
||
34 | |||
35 | /** |
||
36 | * Make extref fields map and set it to parameter |
||
37 | * |
||
38 | * @param ContainerBuilder $container container builder |
||
39 | * @return void |
||
40 | */ |
||
41 | 4 | public function process(ContainerBuilder $container) |
|
42 | { |
||
43 | 4 | $this->documentMap = $container->get('graviton.document.map'); |
|
44 | |||
45 | 4 | $map = []; |
|
46 | |||
47 | 4 | $services = array_keys($container->findTaggedServiceIds('graviton.rest')); |
|
48 | 4 | foreach ($services as $id) { |
|
49 | 4 | list($ns, $bundle, , $doc) = explode('.', $id); |
|
50 | 4 | if (empty($bundle) || empty($doc)) { |
|
51 | continue; |
||
52 | } |
||
53 | 4 | if ($bundle === 'core' && $doc === 'main') { |
|
54 | continue; |
||
55 | } |
||
56 | |||
57 | 4 | $className = $this->getServiceDocument( |
|
58 | 4 | $container->getDefinition($id), |
|
59 | 4 | $ns, |
|
60 | 4 | $bundle, |
|
61 | 2 | $doc |
|
62 | 2 | ); |
|
63 | 4 | $extRefFields = $this->processDocument($this->documentMap->getDocument($className)); |
|
64 | 4 | $routePrefix = strtolower($ns.'.'.$bundle.'.'.'rest'.'.'.$doc); |
|
65 | |||
66 | 4 | $map[$routePrefix.'.get'] = $extRefFields; |
|
67 | 4 | $map[$routePrefix.'.all'] = $extRefFields; |
|
68 | 2 | } |
|
69 | |||
70 | 4 | $container->setParameter('graviton.document.extref.fields', $map); |
|
71 | 4 | } |
|
72 | |||
73 | |||
74 | /** |
||
75 | * Get document class name from service |
||
76 | * |
||
77 | * @param Definition $service Service definition |
||
78 | * @param string $ns Bundle namespace |
||
79 | * @param string $bundle Bundle name |
||
80 | * @param string $doc Document name |
||
81 | * @return string |
||
82 | */ |
||
83 | 4 | View Code Duplication | private function getServiceDocument(Definition $service, $ns, $bundle, $doc) |
102 | |||
103 | /** |
||
104 | * Recursive doctrine document processing |
||
105 | * |
||
106 | * @param Document $document Document |
||
107 | * @param string $exposedPrefix Exposed field prefix |
||
108 | * @return array |
||
109 | */ |
||
110 | 4 | private function processDocument(Document $document, $exposedPrefix = '') |
|
143 | } |
||
144 |