Spider::setMetric()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Finder\Contracts\Spider;
3
4
use Finder\Logic\Output\AbstractOutput;
5
use Finder\Logic\Output\Filter\OutputFilterInterface;
6
use Finder\Logic\Output\TriggerableInterface;
7
8
use Symfony\Component\Finder\Finder;
9
10
use Finder\Spider\File;
11
use Finder\Spider\Directory;
12
use Finder\Spider\Registrator\FileRegistrator;
13
use Finder\Spider\Metrics\FileMetric;
14
15
use Support\Helps\DebugHelper;
16
use Support\Helps\CodeFileHelper;
17
18
/**
19
 * Run all script analysers and outputs their result.
20
 */
21
abstract class Spider extends TargetManager
22
{
23
    protected $registrator = false;
24
    protected $metrics = false;
25
26
    public function __construct($target, $parent = false)
27
    {
28
        parent::__construct($target, $parent);
29
        
30
        $this->registrator = new FileRegistrator($this->getTarget(), $this->getParent());
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Finder\Spider\Regis...(), $this->getParent()) of type object<Finder\Spider\Registrator\FileRegistrator> is incompatible with the declared type boolean of property $registrator.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
31
        $this->setMetric();
32
33
        if ($this->getParent()) {
34
            $this->getMetric()->registerMetricCount('Targets', CodeFileHelper::getClassName($this));
0 ignored issues
show
Bug introduced by
The method registerMetricCount cannot be called on $this->getMetric() (of type boolean).

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...
35
        }
36
    }
37
38
    public function getMetric()
39
    {
40
        return $this->metrics;
41
    }
42
43
    public function setMetric($metricClass = false)
44
    {
45
        if (!$metricClass) {
46
            $metricClass = new FileMetric($this->getTarget(), $this->getParent());
47
        }
48
        $this->metrics = $metricClass;
0 ignored issues
show
Documentation Bug introduced by
It seems like $metricClass can also be of type object<Finder\Spider\Metrics\FileMetric>. However, the property $metrics is declared as type boolean. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
49
    }
50
51
    public function run()
52
    {
53
        $this->analyse();
54
    }
55
56
    public function followChildrens($finder)
57
    {
58
        // check if there are any search results
59
        if (!$finder->hasResults()) {
60
            DebugHelper::info('No Results: '.$this->getTargetPath());
61
62
            return true;
63
        }
64
65
        foreach ($finder as $file) {
66
            if ($file->getType() == 'file') {
67
                $newSpider = new File($file, $this);
0 ignored issues
show
Documentation introduced by
$this is of type this<Finder\Contracts\Spider\Spider>, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
68
            } else {
69
                $newSpider = new Directory($file, $this);
0 ignored issues
show
Documentation introduced by
$this is of type this<Finder\Contracts\Spider\Spider>, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
70
            }
71
            $newSpider->run();
72
73
            $this->getMetric()->mergeWith(
0 ignored issues
show
Bug introduced by
The method mergeWith cannot be called on $this->getMetric() (of type boolean).

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...
74
                $newSpider->getMetric()->saveAndReturnArray()
75
            );
76
        }
77
78
        if (!$this->getParent()) {
79
            dd('Spider', $this->getMetric()->returnMetrics());
0 ignored issues
show
Bug introduced by
The method returnMetrics cannot be called on $this->getMetric() (of type boolean).

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...
80
        }
81
82
        return true;
83
    }
84
}