ConvertErrors::change()   D
last analyzed

Complexity

Conditions 9
Paths 14

Size

Total Lines 42
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 2
Bugs 2 Features 0
Metric Value
c 2
b 2
f 0
dl 0
loc 42
ccs 0
cts 30
cp 0
rs 4.909
cc 9
eloc 27
nc 14
nop 0
crap 90
1
<?php
2
3
use Phinx\Migration\AbstractMigration;
4
use PHPCI\Model\BuildMeta;
5
use PHPCI\Model\BuildError;
6
7
class ConvertErrors extends AbstractMigration
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
8
{
9
    /**
10
     * @var \PHPCI\Store\BuildMetaStore
11
     */
12
    protected $metaStore;
13
14
    /**
15
     * @var \PHPCI\Store\BuildErrorStore
16
     */
17
    protected $errorStore;
18
19
    public function change()
20
    {
21
        $count = 100;
22
23
        $this->metaStore = \b8\Store\Factory::getStore('BuildMeta');
24
        $this->errorStore = \b8\Store\Factory::getStore('BuildError');
25
26
        while ($count == 100) {
27
            $data = $this->metaStore->getErrorsForUpgrade(100);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class b8\Store as the method getErrorsForUpgrade() does only exist in the following sub-classes of b8\Store: PHPCI\Store\BuildMetaStore. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
28
            $count = count($data);
29
30
            /** @var \PHPCI\Model\BuildMeta $meta */
31
            foreach ($data as $meta) {
32
                try {
33
                    switch ($meta->getMetaKey()) {
34
                        case 'phpmd-data':
35
                            $this->processPhpMdMeta($meta);
36
                            break;
37
38
                        case 'phpcs-data':
39
                            $this->processPhpCsMeta($meta);
40
                            break;
41
42
                        case 'phpdoccheck-data':
43
                            $this->processPhpDocCheckMeta($meta);
44
                            break;
45
46
                        case 'phpcpd-data':
47
                            $this->processPhpCpdMeta($meta);
48
                            break;
49
50
                        case 'technicaldebt-data':
51
                            $this->processTechnicalDebtMeta($meta);
52
                            break;
53
                    }
54
                } catch (\Exception $ex) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
55
                }
56
57
                $this->metaStore->delete($meta);
58
            }
59
        }
60
    }
61
62
    protected function processPhpMdMeta(BuildMeta $meta)
63
    {
64
        $data = json_decode($meta->getMetaValue(), true);
65
66
        if (is_array($data) && count($data)) {
67
            foreach ($data as $error) {
68
                $buildError = new BuildError();
69
                $buildError->setBuildId($meta->getBuildId());
70
                $buildError->setPlugin('php_mess_detector');
71
                $buildError->setCreatedDate(new \DateTime());
72
                $buildError->setFile($error['file']);
73
                $buildError->setLineStart($error['line_start']);
74
                $buildError->setLineEnd($error['line_end']);
75
                $buildError->setSeverity(BuildError::SEVERITY_HIGH);
76
                $buildError->setMessage($error['message']);
77
78
                $this->errorStore->save($buildError);
79
            }
80
        }
81
    }
82
83
    protected function processPhpCsMeta(BuildMeta $meta)
84
    {
85
        $data = json_decode($meta->getMetaValue(), true);
86
87
        if (is_array($data) && count($data)) {
88
            foreach ($data as $error) {
89
                $buildError = new BuildError();
90
                $buildError->setBuildId($meta->getBuildId());
91
                $buildError->setPlugin('php_code_sniffer');
92
                $buildError->setCreatedDate(new \DateTime());
93
                $buildError->setFile($error['file']);
94
                $buildError->setLineStart($error['line']);
95
                $buildError->setLineEnd($error['line']);
96
                $buildError->setMessage($error['message']);
97
98
                switch ($error['type']) {
99
                    case 'ERROR':
100
                        $buildError->setSeverity(BuildError::SEVERITY_HIGH);
101
                        break;
102
103
                    case 'WARNING':
104
                        $buildError->setSeverity(BuildError::SEVERITY_LOW);
105
                        break;
106
                }
107
108
                $this->errorStore->save($buildError);
109
            }
110
        }
111
    }
112
113
    protected function processPhpDocCheckMeta(BuildMeta $meta)
114
    {
115
        $data = json_decode($meta->getMetaValue(), true);
116
117
        if (is_array($data) && count($data)) {
118
            foreach ($data as $error) {
119
                $buildError = new BuildError();
120
                $buildError->setBuildId($meta->getBuildId());
121
                $buildError->setPlugin('php_docblock_checker');
122
                $buildError->setCreatedDate(new \DateTime());
123
                $buildError->setFile($error['file']);
124
                $buildError->setLineStart($error['line']);
125
                $buildError->setLineEnd($error['line']);
126
127
                switch ($error['type']) {
128
                    case 'method':
129
                        $buildError->setMessage($error['class'].'::'.$error['method'].' is missing a docblock.');
130
                        $buildError->setSeverity(BuildError::SEVERITY_NORMAL);
131
                        break;
132
133
                    case 'class':
134
                        $buildError->setMessage('Class '.$error['class'].' is missing a docblock.');
135
                        $buildError->setSeverity(BuildError::SEVERITY_LOW);
136
                        break;
137
                }
138
139
                $this->errorStore->save($buildError);
140
            }
141
        }
142
    }
143
144
    protected function processPhpCpdMeta(BuildMeta $meta)
145
    {
146
        $data = json_decode($meta->getMetaValue(), true);
147
148
        if (is_array($data) && count($data)) {
149
            foreach ($data as $error) {
150
                $buildError = new BuildError();
151
                $buildError->setBuildId($meta->getBuildId());
152
                $buildError->setPlugin('php_cpd');
153
                $buildError->setCreatedDate(new \DateTime());
154
                $buildError->setFile($error['file']);
155
                $buildError->setLineStart($error['line_start']);
156
                $buildError->setLineEnd($error['line_end']);
157
                $buildError->setSeverity(BuildError::SEVERITY_NORMAL);
158
                $buildError->setMessage('Copy and paste detected.');
159
160
                $this->errorStore->save($buildError);
161
            }
162
        }
163
    }
164
165
    protected function processTechnicalDebtMeta(BuildMeta $meta)
166
    {
167
        $data = json_decode($meta->getMetaValue(), true);
168
169
        if (is_array($data) && count($data)) {
170
            foreach ($data as $error) {
171
                $buildError = new BuildError();
172
                $buildError->setBuildId($meta->getBuildId());
173
                $buildError->setPlugin('technical_debt');
174
                $buildError->setCreatedDate(new \DateTime());
175
                $buildError->setFile($error['file']);
176
                $buildError->setLineStart($error['line']);
177
                $buildError->setSeverity(BuildError::SEVERITY_NORMAL);
178
                $buildError->setMessage($error['message']);
179
180
                $this->errorStore->save($buildError);
181
            }
182
        }
183
    }
184
}
185