Completed
Push — master ( 04a6d3...a8cd91 )
by Alfred
02:11
created

WritePHPUnitFile   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 117
Duplicated Lines 14.53 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 95.52%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 1
dl 17
loc 117
ccs 64
cts 67
cp 0.9552
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A writeUnitTest() 0 13 1
A checkUnitFolder() 0 12 3
A convertDuskClassAndMethodsArrayToText() 0 16 2
A addParentContent() 9 9 1
A addSteps() 0 14 2
A addNewStepToTest() 0 9 2
A getAndSetHeaderArea() 8 8 1
A getAndSetFooterArea() 0 7 1
A getDuskClassAndMethodsString() 0 4 1
A saveToFile() 0 8 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
namespace GD\Helpers;
4
5
use GD\Exceptions\TestFileExists;
6
7
trait WritePHPUnitFile
8
{
9
10
    protected $dusk_class_and_methods_string = "";
11
    protected $write_destination_folder_path = "";
12
    protected $write_class_name = "";
13
14 21
    public function writeUnitTest($path, $name, $dusk_class_and_methods)
15
    {
16
17 21
        $this->write_class_name = $name;
18
19 21
        $this->write_destination_folder_path = $path;
20
21 21
        $this->checkUnitFolder();
22
23 21
        $this->convertDuskClassAndMethodsArrayToText($dusk_class_and_methods);
24
25 21
        $this->saveToFile();
26 21
    }
27
28 21
    protected function checkUnitFolder()
29
    {
30
31 21
        if (!$this->getFilesystem()->exists($this->write_destination_folder_path)) {
0 ignored issues
show
Bug introduced by
It seems like getFilesystem() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
32
            $this->getFilesystem()->makeDirectory($this->write_destination_folder_path, 0777, true);
0 ignored issues
show
Bug introduced by
It seems like getFilesystem() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
33
        } else {
34 21
            if ($this->getFilesystem()->exists($this->write_destination_folder_path .
0 ignored issues
show
Bug introduced by
It seems like getFilesystem() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
35 21
                $this->getDuskTestName() . '.php')) {
0 ignored issues
show
Bug introduced by
It seems like getDuskTestName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
36
                throw new TestFileExists(sprintf("The file %s already exists", $this->getDuskTestName() . '.php'));
0 ignored issues
show
Bug introduced by
It seems like getDuskTestName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
37
            }
38
        }
39 21
    }
40
41 21
    protected function convertDuskClassAndMethodsArrayToText($dusk_class_and_methods)
42
    {
43
44 21
        $this->getAndSetHeaderArea();
45
46 21
        foreach ($dusk_class_and_methods as $dusk_class_and_method_index => $dusk_class_and_method) {
47
            /**
48
             * Check if set
49
             */
50 21
            $this->addParentContent($dusk_class_and_method['parent']);
51
52 21
            $this->addSteps($dusk_class_and_method['steps']);
53 14
        }
54
55 21
        $this->getAndSetFooterArea();
56 21
    }
57
58 21 View Code Duplication
    protected function addParentContent($parent_function)
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...
59
    {
60 21
        $parent_base = __DIR__ . '/../../stubs/parent.txt';
61 21
        $base = $this->getFilesystem()->get($parent_base);
0 ignored issues
show
Bug introduced by
It seems like getFilesystem() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
62
63 21
        $base = str_replace("[PARENT_METHOD]", $parent_function, $base);
64
65 21
        $this->dusk_class_and_methods_string = $this->dusk_class_and_methods_string . $base;
66 21
    }
67
68 21
    protected function addSteps(array $steps)
69
    {
70 21
        $references = "";
71 21
        $path = __DIR__ . '/../../stubs/step.txt';
72 21
        $step_template = $this->getFilesystem()->get($path);
0 ignored issues
show
Bug introduced by
It seems like getFilesystem() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
73
74 21
        foreach ($steps as $step) {
75 21
            $references = $references . $step['reference'] . ";\n        ";
76 21
            $this->addNewStepToTest($step, $step_template);
77 14
        }
78
79 21
        $this->dusk_class_and_methods_string =
80 21
            str_replace("[STEPS_AREA]", $references, $this->dusk_class_and_methods_string);
81 21
    }
82
83 21
    protected function addNewStepToTest($step, $step_template)
84
    {
85 21
        $method = sprintf("protected function %s()", $step['method_name']);
86 21
        $found = substr_count($this->dusk_class_and_methods_string, $method);
87 21
        if ($found < 1) {
88 21
            $content = str_replace("[STEP]", $step['method_name'], $step_template);
89 21
            $this->dusk_class_and_methods_string = $this->dusk_class_and_methods_string . $content;
90 14
        }
91 21
    }
92
93 21 View Code Duplication
    protected function getAndSetHeaderArea()
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...
94
    {
95 21
        $path = __DIR__ . '/../../stubs/header.txt';
96 21
        $content = $this->getFilesystem()->get($path);
0 ignored issues
show
Bug introduced by
It seems like getFilesystem() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
97
98 21
        $content = str_replace("[TEST_NAME]", $this->write_class_name, $content);
99 21
        $this->dusk_class_and_methods_string = $content;
100 21
    }
101
102 21
    protected function getAndSetFooterArea()
103
    {
104 21
        $path = __DIR__ . '/../../stubs/footer.txt';
105 21
        $content = $this->getFilesystem()->get($path);
0 ignored issues
show
Bug introduced by
It seems like getFilesystem() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
106
107 21
        $this->dusk_class_and_methods_string = $this->dusk_class_and_methods_string . $content;
108 21
    }
109
110 3
    public function getDuskClassAndMethodsString()
111
    {
112 3
        return $this->dusk_class_and_methods_string;
113
    }
114
115 21
    protected function saveToFile()
116
    {
117
118 21
        $this->getFilesystem()->put(
0 ignored issues
show
Bug introduced by
It seems like getFilesystem() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
119 21
            sprintf("%s/%s.php", $this->write_destination_folder_path, $this->write_class_name),
120 21
            $this->dusk_class_and_methods_string
121 14
        );
122 21
    }
123
}
124