SensiolabsInsight   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 167
Duplicated Lines 31.74 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 3
dl 53
loc 167
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A pluginName() 0 4 1
B __construct() 0 27 6
A execute() 11 11 1
A processReport() 32 32 4
A executeSensiolabsInsight() 0 23 1
A wasLastExecSuccessful() 10 10 3

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 Fabrica\Tools\Plugin;
4
5
use Fabrica\Tools;
6
use Fabrica\Tools\Builder;
7
use Fabrica\Models\Infra\Ci\Build;
8
use Fabrica\Tools\Plugin;
9
10
/**
11
 * Sensiolabs Insight Plugin - Allows Sensiolabs Insight testing.
12
 *
13
 * @author Eugen Ganshorn <[email protected]>
14
 */
15
class SensiolabsInsight extends Plugin
16
{
17
    /**
18
     * @var string
19
     */
20
    protected $userUuid;
21
22
    /**
23
     * @var string
24
     */
25
    protected $apiToken;
26
27
    /**
28
     * @var string
29
     */
30
    protected $projectUuid;
31
32
    /**
33
     * @var int
34
     */
35
    protected $allowedWarnings;
36
37
    /**
38
     * @return string
39
     */
40
    public static function pluginName()
41
    {
42
        return 'sensiolabs_insight';
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function __construct(Builder $builder, Build $build, array $options = [])
49
    {
50
        parent::__construct($builder, $build, $options);
51
52
        $this->allowedWarnings = 0;
53
        if (array_key_exists('allowed_warnings', $options)) {
54
            $this->allowedWarnings = (int)$options['allowed_warnings'];
55
        }
56
57
        if (array_key_exists('user_uuid', $options)) {
58
            $this->userUuid = $options['user_uuid'];
59
        }
60
61
        if (array_key_exists('api_token', $options)) {
62
            $this->apiToken = $options['api_token'];
63
        }
64
65
        if (array_key_exists('project_uuid', $options)) {
66
            $this->projectUuid = $options['project_uuid'];
67
        }
68
        
69
        if (array_key_exists('executable', $options)) {
70
            $this->executable = $this->builder->interpolate($options['executable']);
71
        } else {
72
            $this->executable = $this->findBinary('insight');
73
        }
74
    }
75
76
    /**
77
     * Runs Sensiolabs Insights in a specified directory.
78
     *
79
     * @throws \Exception
80
     */
81 View Code Duplication
    public function execute()
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...
82
    {
83
        $insightBinaryPath = $this->executable;
84
85
        $this->executeSensiolabsInsight($insightBinaryPath);
86
87
        $errorCount = $this->processReport(trim($this->builder->getLastOutput()));
88
        $this->build->storeMeta((self::pluginName() . '-warnings'), $errorCount);
89
90
        return $this->wasLastExecSuccessful($errorCount);
91
    }
92
93
    /**
94
     * Process PHPMD's XML output report.
95
     *
96
     * @param $xmlString
97
     *
98
     * @return int
99
     *
100
     * @throws \Exception
101
     */
102 View Code Duplication
    protected function processReport($xmlString)
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...
103
    {
104
        $xml = simplexml_load_string($xmlString);
105
106
        if ($xml === false) {
107
            $this->builder->log($xmlString);
108
            throw new \RuntimeException('Could not process PHPMD report XML.');
109
        }
110
111
        $warnings = 0;
112
113
        foreach ($xml->file as $file) {
114
            $fileName = (string)$file['name'];
115
            $fileName = str_replace($this->builder->buildPath, '', $fileName);
116
117
            foreach ($file->violation as $violation) {
118
                $warnings++;
119
120
                $this->build->reportError(
121
                    $this->builder,
122
                    self::pluginName(),
123
                    (string)$violation,
124
                    PHPCensor\Model\BuildError::SEVERITY_HIGH,
125
                    $fileName,
126
                    (int)$violation['beginline'],
127
                    (int)$violation['endline']
128
                );
129
            }
130
        }
131
132
        return $warnings;
133
    }
134
135
    /**
136
     * Execute Sensiolabs Insight.
137
     *
138
     * @param $binaryPath
139
     */
140
    protected function executeSensiolabsInsight($binaryPath)
141
    {
142
        $cmd = $binaryPath . ' -n analyze --reference %s %s --api-token %s --user-uuid %s';
143
144
        // Run Sensiolabs Insight:
145
        $this->builder->executeCommand(
146
            $cmd,
147
            $this->build->getBranch(),
148
            $this->projectUuid,
149
            $this->apiToken,
150
            $this->userUuid
151
        );
152
153
        $cmd = $binaryPath . ' -n analysis --format pmd %s --api-token %s --user-uuid %s';
154
155
        // Run Sensiolabs Insight:
156
        $this->builder->executeCommand(
157
            $cmd,
158
            $this->projectUuid,
159
            $this->apiToken,
160
            $this->userUuid
161
        );
162
    }
163
164
    /**
165
     * Returns a bool indicating if the error count can be considered a success.
166
     *
167
     * @param int $errorCount
168
     *
169
     * @return bool
170
     */
171 View Code Duplication
    protected function wasLastExecSuccessful($errorCount)
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...
172
    {
173
        $success = true;
174
175
        if ($this->allowedWarnings !== -1 && $errorCount > $this->allowedWarnings) {
176
            $success = false;
177
            return $success;
178
        }
179
        return $success;
180
    }
181
}
182