Completed
Push — master ( 2aea27...e8d42f )
by Nicolai
04:56 queued 02:51
created

BaseTestGenerator   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 157
Duplicated Lines 6.37 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 10
loc 157
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 7

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A getBaseClass() 0 4 1
A prepareSettings() 0 4 1
getBaseSettings() 0 1 ?
A getSuiteSettings() 0 4 1
A getSettingsForModule() 0 4 1
A generate() 0 9 1
A prepareName() 0 6 2
A createFile() 10 10 2
A getPath() 0 4 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
4
namespace SmartWeb\ModuleTesting\Generator;
5
6
use Codeception\Configuration;
7
use Illuminate\Contracts\Filesystem\FileNotFoundException;
8
use Illuminate\Contracts\Filesystem\Filesystem;
9
use Illuminate\Support\Str;
10
use SmartWeb\ModuleTesting\Codeception\SetupInterface;
11
use SmartWeb\ModuleTesting\Template\TemplateFactory;
12
use SmartWeb\ModuleTesting\Util\Template;
13
use const DIRECTORY_SEPARATOR;
14
use function array_merge;
15
16
17
/**
18
 * Class BaseTestGenerator
19
 *
20
 * @package SmartWeb\ModuleTesting\Generator
21
 */
22
abstract class BaseTestGenerator
23
{
24
    
25
    /**
26
     * @var CodeceptionTestType
27
     */
28
    protected $type;
29
    
30
    /**
31
     * @var Filesystem
32
     */
33
    protected $disk;
34
    
35
    /**
36
     * @var SetupInterface
37
     */
38
    protected $setup;
39
    
40
    /**
41
     * @var string
42
     */
43
    protected $suite;
44
    
45
    /**
46
     * @var string
47
     */
48
    protected $name;
49
    
50
    /**
51
     * @var array
52
     */
53
    protected $settings;
54
    
55
    /**
56
     * Key used to identify the template to use for generating test files.
57
     *
58
     * @var string
59
     */
60
    protected $templateKey;
61
    
62
    /**
63
     * BaseTestGenerator constructor.
64
     *
65
     * @param CodeceptionTestType $type
66
     * @param SetupInterface      $setup
67
     * @param string              $suite
68
     * @param string              $name
69
     * @param array               $settings
70
     *
71
     * @throws \Exception
72
     */
73
    public function __construct(CodeceptionTestType $type, SetupInterface $setup, string $suite, string $name, array $settings = [])
74
    {
75
        $this->type = $type;
76
        $this->setup = $setup;
77
        $this->disk = $setup->getDisk();
78
        $this->suite = $suite;
79
        $this->name = $this->prepareName($name);
80
        $this->settings = $settings;
81
        $this->templateKey = $this->type . '.' . Str::lower($this->suite);
82
    }
83
    
84
    /**
85
     * @return string
86
     */
87
    final protected function getBaseClass() : string
88
    {
89
        return Template::getBaseTestClass($this->suite, $this->type);
90
    }
91
    
92
    /**
93
     * @param array $settings
94
     *
95
     * @return array
96
     */
97
    final protected function prepareSettings(array $settings) : array
98
    {
99
        return array_merge($this->getBaseSettings(), $settings);
100
    }
101
    
102
    /**
103
     * @return array
104
     */
105
    abstract protected function getBaseSettings() : array;
106
    
107
    /**
108
     * @param string $suite
109
     *
110
     * @return array
111
     * @throws \Exception
112
     */
113
    final protected function getSuiteSettings(string $suite) : array
114
    {
115
        return Configuration::suiteSettings($suite, $this->getSettingsForModule());
116
    }
117
    
118
    /**
119
     * @return array
120
     * @throws \Codeception\Exception\ConfigurationException
121
     */
122
    final protected function getSettingsForModule()
123
    {
124
        return Configuration::config($this->setup->getWorkDir());
125
    }
126
    
127
    /**
128
     * @throws FileNotFoundException
129
     */
130
    final public function generate()
131
    {
132
        $factory = new TemplateFactory();
133
        $fileContents = $factory->getTemplate($this->templateKey)->produceWith($this->prepareSettings($this->settings));
134
        
135
        $path = $this->suite . DIRECTORY_SEPARATOR . $this->name . Str::ucfirst($this->type) . '.php';
136
        
137
        $this->createFile($path, $fileContents);
138
    }
139
    
140
    /**
141
     * @param string $name
142
     *
143
     * @return string
144
     */
145
    final protected function prepareName(string $name) : string
146
    {
147
        return Str::endsWith($name, Str::ucfirst($this->type))
148
            ? Str::replaceLast(Str::ucfirst($this->type), '', $name)
149
            : $name;
150
    }
151
    
152
    /**
153
     * @param string $path
154
     * @param        $contents
155
     *
156
     * @return bool
157
     */
158 View Code Duplication
    final protected function createFile(string $path, $contents) : bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
159
    {
160
        $path = $this->getPath($path);
161
        
162
        if (!$this->disk->exists($path)) {
163
            return $this->disk->put($path, $contents);
164
        }
165
        
166
        return false;
167
    }
168
    
169
    /**
170
     * @param string $path
171
     *
172
     * @return string
173
     */
174
    final protected function getPath(string $path) : string
175
    {
176
        return $this->setup->getWorkDir() . DIRECTORY_SEPARATOR . $this->setup->getTestsDir() . DIRECTORY_SEPARATOR . $path;
177
    }
178
}
179