Completed
Pull Request — master (#272)
by Kazimieras
02:20
created

TooComplexClassCode::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 4
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Hal\Violation\Class_;
3
4
5
use Hal\Metric\ClassMetric;
6
use Hal\Metric\Metric;
7
use Hal\Violation\Violation;
8
9 View Code Duplication
class TooComplexClassCode implements Violation
0 ignored issues
show
Duplication introduced by
This class 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...
10
{
11
12
    /**
13
     * @inheritdoc
14
     */
15
    public function getName()
16
    {
17
        return 'Too complex class code';
18
    }
19
20
    /**
21
     * @inheritdoc
22
     */
23
    public function apply(Metric $metric)
24
    {
25
        if (!$metric instanceof ClassMetric) {
26
            return;
27
        }
28
29
        $this->metric = $metric;
0 ignored issues
show
Bug introduced by
The property metric does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
30
31
        if ($metric->get('ccn') >= 25) {
32
            $metric->get('violations')->add($this);
0 ignored issues
show
Bug introduced by
The method add cannot be called on $metric->get('violations') (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
33
            return;
34
        }
35
36
    }
37
38
    /**
39
     * @inheritdoc
40
     */
41
    public function getLevel()
42
    {
43
        return Violation::ERROR;
44
    }
45
46
    /**
47
     * @inheritdoc
48
     */
49
    public function getDescription()
50
    {
51
        return <<<EOT
52
This class looks really complex.
53
54
* Algorithms are complex (Total cyclomatic complexity of class is {$this->metric->get('ccn')})
55
* Component uses {$this->metric->get('number_operators')} operators
56
57
Maybe you should delegate some code to other objects.
58
EOT;
59
60
    }
61
}
62