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 |
||
23 | abstract class AbstractInterfaceGenerator extends AbstractGenerator |
||
24 | { |
||
25 | /** @var InterfaceGeneratorFactory */ |
||
26 | protected $interfaceGeneratorFactory; |
||
27 | |||
28 | /** @var DocumentationGeneratorFactory */ |
||
29 | protected $documentationGeneratorFactory; |
||
30 | |||
31 | /** @var FileGeneratorFactory */ |
||
32 | protected $fileGeneratorFactory; |
||
33 | |||
34 | /** @var MethodGeneratorFactory */ |
||
35 | protected $methodGeneratorFactory; |
||
36 | |||
37 | /** |
||
38 | * @param \Net\Bazzline\Component\CodeGenerator\Factory\InterfaceGeneratorFactory $interfaceGeneratorFactory |
||
39 | * @return $this |
||
40 | */ |
||
41 | public function setInterfaceGeneratorFactory(InterfaceGeneratorFactory $interfaceGeneratorFactory) |
||
42 | { |
||
43 | $this->interfaceGeneratorFactory = $interfaceGeneratorFactory; |
||
44 | |||
45 | return $this; |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * @param \Net\Bazzline\Component\CodeGenerator\Factory\DocumentationGeneratorFactory $documentationGeneratorFactory |
||
50 | * @return $this |
||
51 | */ |
||
52 | public function setDocumentationGeneratorFactory(DocumentationGeneratorFactory $documentationGeneratorFactory) |
||
53 | { |
||
54 | $this->documentationGeneratorFactory = $documentationGeneratorFactory; |
||
55 | |||
56 | return $this; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @param \Net\Bazzline\Component\CodeGenerator\Factory\FileGeneratorFactory $fileGeneratorFactory |
||
61 | * @return $this |
||
62 | */ |
||
63 | public function setFileGeneratorFactory(FileGeneratorFactory $fileGeneratorFactory) |
||
64 | { |
||
65 | $this->fileGeneratorFactory = $fileGeneratorFactory; |
||
66 | |||
67 | return $this; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * @param \Net\Bazzline\Component\CodeGenerator\Factory\MethodGeneratorFactory $methodGeneratorFactory |
||
72 | * @return $this |
||
73 | */ |
||
74 | public function setMethodGeneratorFactory(MethodGeneratorFactory $methodGeneratorFactory) |
||
75 | { |
||
76 | $this->methodGeneratorFactory = $methodGeneratorFactory; |
||
77 | |||
78 | return $this; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @throws RuntimeException |
||
83 | */ |
||
84 | public function generate() |
||
85 | { |
||
86 | $this->generateInterface( |
||
87 | $this->getInterfaceName(), |
||
88 | $this->configuration->getFileNameExtension() |
||
89 | ); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @param string $name |
||
94 | * @param InterfaceGenerator $interfaceGenerator |
||
95 | * @param Configuration $configuration |
||
96 | * @param DocumentationGeneratorFactory $documentationGeneratorFactory |
||
97 | * @param MethodGeneratorFactory $methodGeneratorFactory |
||
98 | * @return InterfaceGenerator |
||
99 | */ |
||
100 | abstract protected function createInterface($name, InterfaceGenerator $interfaceGenerator, Configuration $configuration, DocumentationGeneratorFactory $documentationGeneratorFactory, MethodGeneratorFactory $methodGeneratorFactory); |
||
101 | |||
102 | /** |
||
103 | * @return string |
||
104 | */ |
||
105 | abstract protected function getInterfaceName(); |
||
106 | |||
107 | /** |
||
108 | * @param string $name |
||
109 | * @param string $extension |
||
110 | * @throws RuntimeException |
||
111 | */ |
||
112 | protected function generateInterface($name, $extension) |
||
133 | |||
134 | /** |
||
135 | * @param FileGenerator $fileGenerator |
||
136 | * @return FileGenerator |
||
137 | */ |
||
138 | View Code Duplication | private function createFile(FileGenerator $fileGenerator) |
|
151 | } |