SecretSoftware   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 108
Duplicated Lines 14.81 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 1
dl 16
loc 108
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A underScrutiny() 0 10 3
A force() 0 16 2
A forcePropertyValue() 8 8 1
A getPropertyValue() 8 8 1
A accessProperty() 0 7 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 HMLB\VW;
4
5
use PHPUnit_Framework_TestCase;
6
use PHPUnit_Runner_BaseTestRunner;
7
use ReflectionClass;
8
use ReflectionProperty;
9
10
/**
11
 * You didn't see that.
12
 *
13
 * I hope you don't understand this code.
14
 *
15
 * @author Hugues Maignol <[email protected]>
16
 * @author Martin Winterkorn <[email protected]>
17
 */
18
class SecretSoftware
19
{
20
    private $examinators = array(
21
        'CI',
22
        'CONTINUOUS_INTEGRATION',
23
        'BUILD_ID',
24
        'BUILD_NUMBER',
25
        'TEAMCITY_VERSION',
26
        'TRAVIS',
27
        'CIRCLECI',
28
        'JENKINS_URL',
29
        'HUDSON_URL',
30
        'bamboo.buildKey',
31
        'PHPCI',
32
        'GOCD_SERVER_HOST',
33
        'BUILDKITE',
34
    );
35
36
    public function __construct(array $additionalEnvVariables = array())
37
    {
38
        $this->examinators = array_merge($this->examinators, $additionalEnvVariables);
39
    }
40
41
    /**
42
     * Where the magic occurs.
43
     *
44
     * @return bool
45
     */
46
    public function underScrutiny()
47
    {
48
        foreach ($this->examinators as $gaze) {
49
            if (getenv($gaze)) {
50
                return true;
51
            }
52
        }
53
54
        return false;
55
    }
56
57
    /**
58
     * Failing test cases are not a problem anymore.
59
     *
60
     * @param PHPUnit_Framework_TestCase $test
61
     */
62
    public function force(PHPUnit_Framework_TestCase $test)
63
    {
64
        if (!$test->hasFailed()) {
65
            return;
66
        }
67
        $testReflection = new ReflectionClass('PHPUnit_Framework_TestCase');
68
        $resultReflection = new ReflectionClass('PHPUnit_Framework_TestResult');
69
70
        $result = $this->getPropertyValue($testReflection, 'result', $test);
71
        $this
72
            ->forcePropertyValue($resultReflection, 'errors', array(), $result)
73
            ->forcePropertyValue($resultReflection, 'failures', array(), $result)
74
            ->forcePropertyValue($resultReflection, 'risky', array(), $result)
75
            ->forcePropertyValue($testReflection, 'status', PHPUnit_Runner_BaseTestRunner::STATUS_PASSED, $test)
76
            ->forcePropertyValue($testReflection, 'statusMessage', '', $test);
77
    }
78
79
    /**
80
     * @param ReflectionClass $reflection
81
     * @param string          $property
82
     * @param mixed           $value
83
     * @param mixed           $object
84
     *
85
     * @return self
86
     */
87 View Code Duplication
    private function forcePropertyValue(ReflectionClass $reflection, $property, $value, $object)
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...
88
    {
89
        $propertyReflection = $this->accessProperty($reflection, $property);
90
        $propertyReflection->setValue($object, $value);
91
        $propertyReflection->setAccessible(false);
92
93
        return $this;
94
    }
95
96
    /**
97
     * @param ReflectionClass $reflection
98
     * @param string          $property
99
     * @param mixed           $object
100
     *
101
     * @return mixed
102
     */
103 View Code Duplication
    private function getPropertyValue(ReflectionClass $reflection, $property, $object)
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...
104
    {
105
        $propertyReflection = $this->accessProperty($reflection, $property);
106
        $value = $propertyReflection->getValue($object);
107
        $propertyReflection->setAccessible(false);
108
109
        return $value;
110
    }
111
112
    /**
113
     * @param ReflectionClass $reflection
114
     * @param string          $property
115
     *
116
     * @return ReflectionProperty
117
     */
118
    private function accessProperty(ReflectionClass $reflection, $property)
119
    {
120
        $propertyReflection = $reflection->getProperty($property);
121
        $propertyReflection->setAccessible(true);
122
123
        return $propertyReflection;
124
    }
125
}
126