AbstractInterfaceGenerator   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 129
Duplicated Lines 10.08 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 13
loc 129
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
createInterface() 0 1 ?
getInterfaceName() 0 1 ?
A setInterfaceGeneratorFactory() 0 6 1
A setDocumentationGeneratorFactory() 0 6 1
A setFileGeneratorFactory() 0 6 1
A setMethodGeneratorFactory() 0 6 1
A generate() 0 7 1
A generateInterface() 0 21 1
A createFile() 13 13 1

How to fix   Duplicated Code   

Duplicated Code

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
2
/**
3
 * @author stev leibelt <[email protected]>
4
 * @since 2014-12-23 
5
 */
6
7
namespace Net\Bazzline\Component\Locator\Generator;
8
9
use Net\Bazzline\Component\CodeGenerator\InterfaceGenerator;
10
use Net\Bazzline\Component\CodeGenerator\Factory\InterfaceGeneratorFactory;
11
use Net\Bazzline\Component\CodeGenerator\Factory\DocumentationGeneratorFactory;
12
use Net\Bazzline\Component\CodeGenerator\Factory\FileGeneratorFactory;
13
use Net\Bazzline\Component\CodeGenerator\Factory\MethodGeneratorFactory;
14
use Net\Bazzline\Component\CodeGenerator\FileGenerator;
15
use Net\Bazzline\Component\Locator\Configuration\Configuration;
16
use Net\Bazzline\Component\Locator\Generator\AbstractGenerator;
17
use Net\Bazzline\Component\Locator\RuntimeException;
18
19
/**
20
 * Class AbstractInterfaceGenerator
21
 * @package Net\Bazzline\Component\Locator
22
 */
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)
113
    {
114
        $fileName = $name . $extension;
115
        $this->moveOldFileIfExists($this->configuration->getFilePath(), $fileName);
116
117
        $fileGenerator = $this->createFile($this->fileGeneratorFactory->create());
118
        $interfaceGenerator = $this->createInterface(
119
            $name,
120
            $this->interfaceGeneratorFactory->create(),
121
            $this->configuration,
122
            $this->documentationGeneratorFactory,
123
            $this->methodGeneratorFactory
124
        );
125
126
        $fileGenerator->addInterface($interfaceGenerator);
127
        $fileContent = $fileGenerator->generate();
128
129
        $fullQualifiedPathName = $this->configuration->getFilePath() .
130
            DIRECTORY_SEPARATOR . $fileName;
131
        $this->dumpToFile($fullQualifiedPathName, $fileContent);
132
    }
133
134
    /**
135
     * @param FileGenerator $fileGenerator
136
     * @return FileGenerator
137
     */
138 View Code Duplication
    private function createFile(FileGenerator $fileGenerator)
139
    {
140
        $fileGenerator->addFileContent(
141
            array(
142
                '/**',
143
                ' * @author ' . $this->getAuthorString(),
144
                ' * @since ' . date('Y-m-d'),
145
                ' */'
146
            )
147
        );
148
149
        return $fileGenerator;
150
    }
151
}