JunitHelper   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 121
Duplicated Lines 16.53 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 0
dl 20
loc 121
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A generateJunitFile() 0 18 2
A createTestSuite() 0 16 2
B createTestCase() 10 23 5
A createElement() 0 10 2
A sumArraysKey() 10 10 3
A sumDomChildsAttribute() 0 9 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
 * This file is part of project-quality-inspector.
4
 *
5
 * (c) Alexandre GESLIN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace ProjectQualityInspector\Application\Output;
12
13
use ProjectQualityInspector\Exception\RuleViolationException;
14
use ProjectQualityInspector\Iterator\RuleFilterIterator;
15
16
class JunitHelper
17
{
18
    /**
19
     * @param  RuleInterface[]  $rules
20
     * @param  string $junitFile
21
     */
22
    public static function generateJunitFile(RuleFilterIterator $rules, $junitFile)
23
    {
24
        $xml = new \DOMDocument('1.0', 'utf-8');
25
        $testSuites = $xml->createElement("testsuites");
26
27
        foreach ($rules as $rule) {
28
            $testSuites->appendChild(static::createTestSuite($rule::getRuleName(), $rule->getAssertions(), $xml));
0 ignored issues
show
Bug introduced by
Since createTestSuite() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of createTestSuite() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
29
        }
30
31
      $testSuites->setAttribute('name', 'pqi');
32
      $testSuites->setAttribute('errors', static::sumDomChildsAttribute($testSuites, 'errors'));
0 ignored issues
show
Bug introduced by
Since sumDomChildsAttribute() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of sumDomChildsAttribute() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
33
      $testSuites->setAttribute('failures', static::sumDomChildsAttribute($testSuites, 'failures'));
0 ignored issues
show
Bug introduced by
Since sumDomChildsAttribute() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of sumDomChildsAttribute() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
34
      $testSuites->setAttribute('time', static::sumDomChildsAttribute($testSuites, 'time'));
0 ignored issues
show
Bug introduced by
Since sumDomChildsAttribute() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of sumDomChildsAttribute() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
35
36
        $xml->appendChild($testSuites);
37
38
        file_put_contents($junitFile, $xml->saveXML());
39
    }
40
41
    /**
42
     * @param  array $tests
43
     * @param  DOMDocument $xml
44
     * @return DOMDocument
45
     */
46
    private static function createTestSuite($name, array $tests, \DOMDocument $xml)
47
    {
48
        $testSuite = static::createElement('testsuite', [
0 ignored issues
show
Bug introduced by
Since createElement() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of createElement() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
49
            'name' => sprintf('pqi.%s', $name),
50
            'tests' => count($tests),
51
            'failures' => static::sumArraysKey('failures', $tests, 'sum'),
0 ignored issues
show
Bug introduced by
Since sumArraysKey() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of sumArraysKey() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
52
            'errors' => static::sumArraysKey('errors', $tests, 'sum'),
0 ignored issues
show
Bug introduced by
Since sumArraysKey() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of sumArraysKey() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
53
            'time' => static::sumArraysKey('time', $tests)
0 ignored issues
show
Bug introduced by
Since sumArraysKey() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of sumArraysKey() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
54
        ], $xml);
55
56
        foreach ($tests as $test) {
57
            $testSuite->appendChild(static::createTestCase($test, $xml));
0 ignored issues
show
Bug introduced by
Since createTestCase() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of createTestCase() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
58
        }
59
60
        return $testSuite;
61
    }
62
63
    private static function createTestCase(array $test, \DOMDocument $xml)
64
    {
65
        $testCase = $xml->createElement('testcase');
66
        $testCase->setAttribute('name', $test['name']);
67
        $testCase->setAttribute('assertions', $test['assertions']);
68
        $testCase->setAttribute('classname', $test['classname']);
69
        $testCase->setAttribute('status', $test['status']);
70
        $testCase->setAttribute('time', $test['time']);
71
72 View Code Duplication
        if ($test['failures']['sum'] > 0) {
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...
73
            foreach ($test['failures']['list']  as $failure) {
74
                $testCase->appendChild(static::createElement('failure', ['type' => $failure['type']], $xml, $failure['message']));
0 ignored issues
show
Bug introduced by
Since createElement() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of createElement() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
75
            }
76
        }
77
78 View Code Duplication
        if ($test['errors']['sum'] > 0) {
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...
79
            foreach ($test['errors']['list'] as $error) {
80
                $testCase->appendChild(static::createElement('error', ['type' => $error['type']], $xml, $failure['message']));
0 ignored issues
show
Bug introduced by
Since createElement() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of createElement() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
Bug introduced by
The variable $failure seems to be defined by a foreach iteration on line 73. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
81
            }
82
        }
83
84
        return $testCase;
85
    }
86
87
    /**
88
     * @param  string $tagName
89
     * @param  array $attributes
90
     * @param  \DOMDocument $xml
91
     * @param  string $value
92
     * @return \DOMDocument
93
     */
94
    private static function createElement($tagName, array $attributes, \DOMDocument $xml, $value = null)
95
    {
96
        $element = $xml->createElement($tagName, $value);
97
        
98
        foreach ($attributes as $key => $value) {
99
            $element->setAttribute($key, $value);
100
        }
101
102
        return $element;
103
    }
104
105
    /**
106
     * @param  string $key
107
     * @param  array  $arrays
108
     * @param  string  $subKey
109
     * @return integer
110
     */
111 View Code Duplication
    private static function sumArraysKey($key, array $arrays, $subKey = null)
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...
112
    {
113
        $sum = 0;
114
115
        foreach ($arrays as $array) {
116
            $sum += ($subKey) ? $array[$key][$subKey] : $array[$key];
117
        }
118
119
        return $sum;
120
    }
121
122
    /**
123
     * @param  \DOMElement $element
124
     * @param  string $attribute
125
     * @return integer
126
     */
127
    private static function sumDomChildsAttribute(\DOMElement $element, $attribute)
128
    {
129
        $sum = 0;
130
        foreach ($element->childNodes as $childElement) {
131
            $sum += (int) $childElement->getAttribute($attribute);
132
        }
133
134
        return $sum;
135
    }
136
}