PhpDocblockChecker::execute()   B
last analyzed

Complexity

Conditions 9
Paths 64

Size

Total Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 55
rs 7.4262
c 0
b 0
f 0
cc 9
nc 64
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Models\Infra\Ci\BuildError;
9
use Fabrica\Tools\Plugin;
10
use Fabrica\Tools\ZeroConfigPluginInterface;
11
12
/**
13
 * PHP Docblock Checker Plugin - Checks your PHP files for appropriate uses of Docblocks
14
 *
15
 * @author Ricardo Sierra <[email protected]>
16
 */
17
class PhpDocblockChecker extends Plugin implements ZeroConfigPluginInterface
18
{
19
    protected $skipClasses    = false;
20
    protected $skipMethods    = false;
21
    protected $skipSignatures = false;
22
    /**
23
     * @var int
24
     */
25
    protected $allowedWarnings;
26
27
    /**
28
     * @return string
29
     */
30
    public static function pluginName()
31
    {
32
        return 'php_docblock_checker';
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function __construct(Builder $builder, Build $build, array $options = [])
39
    {
40
        parent::__construct($builder, $build, $options);
41
42
        $this->allowedWarnings = 0;
43
44
        if (isset($options['zero_config']) && $options['zero_config']) {
45
            $this->allowedWarnings = -1;
46
        }
47
48
        if (array_key_exists('skip_classes', $options)) {
49
            $this->skipClasses = true;
50
        }
51
52
        if (array_key_exists('skip_methods', $options)) {
53
            $this->skipMethods = true;
54
        }
55
56
        if (array_key_exists('skip_signatures', $options)) {
57
            $this->skipSignatures = true;
58
        }
59
60
        if (array_key_exists('allowed_warnings', $options)) {
61
            $this->allowedWarnings = (int)$options['allowed_warnings'];
62
        }
63
64
        $this->executable = $this->findBinary(
65
            [
66
            'phpdoc-checker',
67
            'phpdoc-checker.phar',
68
            ]
69
        );
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public static function canExecuteOnStage($stage, Build $build)
76
    {
77
        if (Build::STAGE_TEST === $stage) {
78
            return true;
79
        }
80
81
        return false;
82
    }
83
84
    /**
85
     * Runs PHP Mess Detector in a specified directory.
86
     */
87
    public function execute()
88
    {
89
        // Check that the binary exists:
90
        $checkerCmd = $this->executable;
91
92
        // Build ignore string:
93
        $ignore = '';
94
        if (is_array($this->ignore)) {
95
            $ignore = sprintf(' --exclude="%s"', implode(',', $this->ignore));
96
        }
97
98
        // Are we skipping any checks?
99
        $add = '';
100
        if ($this->skipClasses) {
101
            $add .= ' --skip-classes';
102
        }
103
104
        if ($this->skipMethods) {
105
            $add .= ' --skip-methods';
106
        }
107
108
        if ($this->skipSignatures) {
109
            $add .= ' --skip-signatures';
110
        }
111
112
        // Build command string:
113
        $cmd = $checkerCmd . ' --json --directory="%s"%s%s';
114
115
        // Run checker:
116
        $this->builder->executeCommand(
117
            $cmd,
118
            $this->directory,
119
            $ignore,
120
            $add
121
        );
122
123
        $output = json_decode($this->builder->getLastOutput(), true);
124
125
        $errors = 0;
126
        if ($output && is_array($output)) {
127
            $errors = count($output);
128
            $this->builder->logWarning("Number of error : " . $errors);
129
130
            $this->reportErrors($output);
131
        }
132
        $this->build->storeMeta((self::pluginName() . '-warnings'), $errors);
133
134
        $success = true;
135
136
        if (-1 != $this->allowedWarnings && $errors > $this->allowedWarnings) {
137
            $success = false;
138
        }
139
140
        return $success;
141
    }
142
143
    /**
144
     * Report all of the errors we've encountered line-by-line.
145
     *
146
     * @param array $output
147
     */
148
    protected function reportErrors(array $output)
149
    {
150
        foreach ($output as $error) {
151
            switch ($error['type']) {
152
            case 'class':
153
                $message  = 'Class ' . $error['class'] . ' is missing a docblock.';
154
                $severity = BuildError::SEVERITY_NORMAL;
155
                break;
156
157 View Code Duplication
            case 'method':
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...
158
                $message  = 'Method ' . $error['class'] . '::' . $error['method'] . ' is missing a docblock.';
159
                $severity = BuildError::SEVERITY_NORMAL;
160
                break;
161
162 View Code Duplication
            case 'param-missing':
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...
163
                $message  = $error['class'] . '::' . $error['method'] . ' @param ' . $error['param'] . ' missing.';
164
                $severity = BuildError::SEVERITY_LOW;
165
                break;
166
167
            case 'param-mismatch':
168
                $message = $error['class'] . '::' . $error['method'] . ' @param ' . $error['param'] .
169
                    '(' . $error['doc-type'] . ') does not match method signature (' . $error['param-type'] . ')';
170
                $severity = BuildError::SEVERITY_LOW;
171
                break;
172
173 View Code Duplication
            case 'return-missing':
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...
174
                $message  = $error['class'] . '::' . $error['method'] . ' @return missing.';
175
                $severity = BuildError::SEVERITY_LOW;
176
                break;
177
178
            case 'return-mismatch':
179
                $message = $error['class'] . '::' . $error['method'] . ' @return ' . $error['doc-type'] .
180
                    ' does not match method signature (' . $error['return-type'] . ')';
181
                $severity = BuildError::SEVERITY_LOW;
182
                break;
183
184
            default:
185
                $message  = 'Class ' . $error['class'] . ' invalid/missing a docblock.';
186
                $severity = BuildError::SEVERITY_LOW;
187
                break;
188
            }
189
190
            $this->build->reportError(
191
                $this->builder,
192
                self::pluginName(),
193
                $message,
194
                $severity,
195
                $error['file'],
196
                $error['line']
197
            );
198
        }
199
    }
200
}
201