WriteFileBase   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 175
Duplicated Lines 2.29 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 83.13%

Importance

Changes 0
Metric Value
wmc 25
c 0
b 0
f 0
lcom 1
cbo 2
dl 4
loc 175
ccs 69
cts 83
cp 0.8313
rs 10

16 Methods

Rating   Name   Duplication   Size   Complexity  
A getWriteClassName() 0 4 1
A setWriteClassName() 0 4 1
A getFilesystem() 0 7 2
A setFilesystem() 0 9 2
A convertDuskClassAndMethodsArrayToText() 0 16 2
A getDuskClassAndMethodsString() 0 4 1
A getAndSetHeaderArea() 0 5 1
getAndSetFooterArea() 0 1 ?
A addSteps() 0 12 2
A notLastLine() 0 8 2
A addParentContent() 0 6 1
A addNewStepToTest() 4 10 2
A checkDestinationTestFolder() 0 12 3
A saveToFile() 0 9 1
A getExistingTestContent() 0 9 2
A setExistingTestContent() 0 11 2

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 GD\Helpers;
5
6
use GD\Exceptions\TestFileExists;
7
use Illuminate\Filesystem\Filesystem;
8
9
abstract class WriteFileBase
10
{
11
12
    protected $write_destination_folder_path = "";
13
    protected $dusk_class_and_methods_string = "";
14
    protected $write_class_name = "";
15
    protected $spacing = "";
16
    protected $existing_test_content;
17
18
    /**
19
     * @var Filesystem
20
     */
21
    protected $filesystem;
22
    protected $base;
23
    protected $step_template;
24
    protected $content;
25
26
    /**
27
     * @return string
28
     */
29
    public function getWriteClassName()
30
    {
31
        return $this->write_class_name;
32
    }
33
34
    public function setWriteClassName($name)
35
    {
36
        $this->write_class_name = $name;
37
    }
38
39
40
    /**
41
     * @return Filesystem
42
     */
43 48
    public function getFilesystem()
44
    {
45 48
        if (!$this->filesystem) {
46 48
            $this->setFilesystem();
47 32
        }
48 48
        return $this->filesystem;
49
    }
50
51
    /**
52
     * @param null $filesystem
53
     * @return $this
54
     */
55 48
    public function setFilesystem($filesystem = null)
56
    {
57 48
        if (!$filesystem) {
58 48
            $filesystem = new Filesystem();
59 32
        }
60
61 48
        $this->filesystem = $filesystem;
62 48
        return $this;
63
    }
64
65 24
    protected function convertDuskClassAndMethodsArrayToText($dusk_class_and_methods)
66
    {
67
68 24
        $this->getAndSetHeaderArea();
69
70 24
        foreach ($dusk_class_and_methods as $dusk_class_and_method_index => $dusk_class_and_method) {
71
            /**
72
             * Check if set
73
             */
74 24
            $this->addParentContent($dusk_class_and_method['parent']);
75
76 24
            $this->addSteps($dusk_class_and_method['steps']);
77 16
        }
78
79 24
        $this->getAndSetFooterArea();
80 24
    }
81
82 9
    public function getDuskClassAndMethodsString()
83
    {
84 9
        return $this->dusk_class_and_methods_string;
85
    }
86
87 24
    protected function getAndSetHeaderArea()
88
    {
89 24
        $content = str_replace("[TEST_NAME]", $this->write_class_name, $this->content);
90 24
        $this->dusk_class_and_methods_string = $content;
91 24
    }
92
93
    abstract protected function getAndSetFooterArea();
94
95 42
    protected function addSteps(array $steps)
96
    {
97 42
        $references = "";
98
99 42
        foreach ($steps as $index => $step) {
100 42
            $references = $references . $this->spacing . $step['reference'] . $this->notLastLine($steps, $index);
101 42
            $this->addNewStepToTest($step, $this->step_template);
102 28
        }
103
104 42
        $this->dusk_class_and_methods_string =
105 42
            str_replace("[STEPS_AREA]", $references, $this->dusk_class_and_methods_string);
106 42
    }
107
108 42
    protected function notLastLine(array $steps, $index)
109
    {
110 42
        if (($index + 1) < count($steps)) {
111 42
            return ";\n        ";
112
        }
113
114 42
        return ";";
115
    }
116
117
118 42
    protected function addParentContent($parent_function)
119
    {
120 42
        $base = str_replace("[PARENT_METHOD]", $parent_function, $this->base);
121
122 42
        $this->dusk_class_and_methods_string = $this->dusk_class_and_methods_string . $base;
123 42
    }
124
125 24
    protected function addNewStepToTest($step, $step_template)
126
    {
127 24
        $method = sprintf("protected function %s()", $step['method_name']);
128 24
        $found = substr_count($this->dusk_class_and_methods_string, $method);
129
130 24 View Code Duplication
        if ($found < 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
131 24
            $content = str_replace(["[STEP]", "[METHOD]"], $step['method_name'], $step_template);
132 24
            $this->dusk_class_and_methods_string = $this->dusk_class_and_methods_string . $content;
133 16
        }
134 24
    }
135
136 24
    protected function checkDestinationTestFolder()
137
    {
138
139 24
        if (!$this->getFilesystem()->exists($this->write_destination_folder_path)) {
140
            $this->getFilesystem()->makeDirectory($this->write_destination_folder_path, 0777, true);
141
        } else {
142 24
            if ($this->getFilesystem()->exists($this->write_destination_folder_path .
143 24
                $this->write_class_name . '.php')) {
144
                throw new TestFileExists(sprintf("The file %s already exists", $this->write_class_name . '.php'));
145
            }
146
        }
147 24
    }
148
149 42
    protected function saveToFile()
150
    {
151 42
        $path = sprintf("%s/%s.php", $this->write_destination_folder_path, $this->write_class_name);
152
153 42
        $this->getFilesystem()->put(
154 42
            $path,
155 42
            $this->dusk_class_and_methods_string
156 28
        );
157 42
    }
158
159 18
    public function getExistingTestContent()
160
    {
161
162 18
        if (!$this->existing_test_content) {
163
            $this->setExistingTestContent();
164
        }
165
        
166 18
        return $this->existing_test_content;
167
    }
168
169
    /**
170
     * @param mixed $existing_test_content
171
     */
172 18
    public function setExistingTestContent($existing_test_content = null)
173
    {
174 18
        if (!$existing_test_content) {
175
            $file_to_append = $this->write_destination_folder_path . '/' . $this->getWriteClassName() . '.php';
176
177
            $content = $this->getFilesystem()->get($file_to_append);
178
            $this->existing_test_content = $content;
179
        }
180
        
181 18
        $this->existing_test_content = $existing_test_content;
182 18
    }
183
}
184