Completed
Push — master ( 5c4f5a...443735 )
by Alfred
02:19
created

WritePHPUnitFile   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 113
Duplicated Lines 15.04 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 95.24%

Importance

Changes 0
Metric Value
wmc 15
c 0
b 0
f 0
lcom 1
cbo 1
dl 17
loc 113
ccs 60
cts 63
cp 0.9524
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A writeUnitTest() 0 13 1
A checkUnitFolder() 0 11 3
A convertDuskClassAndMethodsArrayToText() 0 16 2
A addParentContent() 9 9 1
A addSteps() 0 13 2
A addNewStepToTest() 0 7 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 . $this->getDuskTestName() . '.php')) {
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...
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...
35
                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...
36
            }
37
        }
38 21
    }
39
40 21
    protected function convertDuskClassAndMethodsArrayToText($dusk_class_and_methods)
41
    {
42
43 21
        $this->getAndSetHeaderArea();
44
45 21
        foreach ($dusk_class_and_methods as $dusk_class_and_method_index => $dusk_class_and_method) {
46
            /**
47
             * Check if set
48
             */
49 21
            $this->addParentContent($dusk_class_and_method['parent']);
50
51 21
            $this->addSteps($dusk_class_and_method['steps']);
52 14
        }
53
54 21
        $this->getAndSetFooterArea();
55 21
    }
56
57 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...
58
    {
59 21
        $parent_base = __DIR__ . '/../../stubs/parent.txt';
60 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...
61
62 21
        $base = str_replace("[PARENT_METHOD]", $parent_function, $base);
63
64 21
        $this->dusk_class_and_methods_string = $this->dusk_class_and_methods_string . $base;
65 21
    }
66
67 21
    protected function addSteps(array $steps)
68
    {
69 21
        $references = "";
70 21
        $path = __DIR__ . '/../../stubs/step.txt';
71 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...
72
73 21
        foreach ($steps as $step) {
74 21
            $references = $references . $step['reference'] . ";\n        ";
75 21
            $this->addNewStepToTest($step, $step_template);
76 14
        }
77
78 21
        $this->dusk_class_and_methods_string = str_replace("[STEPS_AREA]", $references, $this->dusk_class_and_methods_string);
79 21
    }
80
81 21
    protected function addNewStepToTest($step, $step_template)
82
    {
83 21
        if (substr_count($this->dusk_class_and_methods_string, sprintf("protected function %s()", $step['method_name'])) < 1) {
84 21
            $content = str_replace("[STEP]", $step['method_name'], $step_template);
85 21
            $this->dusk_class_and_methods_string = $this->dusk_class_and_methods_string . $content;
86 14
        }
87 21
    }
88
89 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...
90
    {
91 21
        $path = __DIR__ . '/../../stubs/header.txt';
92 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...
93
94 21
        $content = str_replace("[TEST_NAME]", $this->write_class_name, $content);
95 21
        $this->dusk_class_and_methods_string = $content;
96 21
    }
97
98 21
    protected function getAndSetFooterArea()
99
    {
100 21
        $path = __DIR__ . '/../../stubs/footer.txt';
101 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...
102
103 21
        $this->dusk_class_and_methods_string = $this->dusk_class_and_methods_string . $content;
104 21
    }
105
106 3
    public function getDuskClassAndMethodsString()
107
    {
108 3
        return $this->dusk_class_and_methods_string;
109
    }
110
111 21
    protected function saveToFile()
112
    {
113
114 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...
115 21
            sprintf("%s/%s.php", $this->write_destination_folder_path, $this->write_class_name),
116 21
            $this->dusk_class_and_methods_string
117 14
        );
118 21
    }
119
}
120