Passed
Push — master ( d07941...1d8911 )
by Florian
02:11 queued 10s
created

_getServiceAnnotations()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 4
nop 1
dl 0
loc 19
ccs 0
cts 11
cp 0
crap 20
rs 9.9332
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace Burzum\Cake\Annotator\ClassAnnotatorTask;
5
6
use Cake\Core\App;
7
use IdeHelper\Annotation\AnnotationFactory;
8
use IdeHelper\Annotator\ClassAnnotatorTask\AbstractClassAnnotatorTask;
9
use IdeHelper\Annotator\ClassAnnotatorTask\ClassAnnotatorTaskInterface;
10
11
/**
12
 * Classes that use ServiceAwareTrait should automatically have used tables - via loadService() call - annotated.
13
 */
14
class ServiceAwareClassAnnotatorTask extends AbstractClassAnnotatorTask implements ClassAnnotatorTaskInterface
15
{
16
    /**
17
     * @param string $path Path
18
     * @param string $content Content
19
     * @return bool
20
     */
21
    public function shouldRun($path, $content)
22
    {
23
        if (!preg_match('#\buse ServiceAwareTrait\b#', $content)) {
24
            return false;
25
        }
26
27
        return true;
28
    }
29
30
    /**
31
     * @param string $path Path
32
     * @return bool
33
     */
34
    public function annotate($path)
35
    {
36
        $services = $this->_getUsedServices($this->content);
37
38
        $annotations = $this->_getServiceAnnotations($services);
39
40
        return $this->_annotate($path, $this->content, $annotations);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->_annotate(...>content, $annotations) returns the type boolean which is incompatible with the return type mandated by IdeHelper\Annotator\Clas...skInterface::annotate() of void.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
41
    }
42
43
    /**
44
     * @param string $content Content
45
     *
46
     * @return array
47
     */
48
    protected function _getUsedServices($content)
49
    {
50
        preg_match_all('/\$this-\>loadService\(\'([a-z.\\/]+)\'/i', $content, $matches);
51
        if (empty($matches[1])) {
52
            return [];
53
        }
54
55
        $services = $matches[1];
56
57
        return array_unique($services);
58
    }
59
60
    /**
61
     * @param array $usedServices Used services
62
     * @return \IdeHelper\Annotation\AbstractAnnotation[]
63
     */
64
    protected function _getServiceAnnotations($usedServices)
65
    {
66
        $annotations = [];
67
68
        foreach ($usedServices as $usedService) {
69
            $className = App::className($usedService, 'Service', 'Service');
70
            if (!$className) {
71
                continue;
72
            }
73
            list(, $name) = pluginSplit($usedService);
74
75
            if (strpos($name, '/') !== false) {
76
                $name = substr($name, strrpos($name, '/') + 1);
77
            }
78
79
            $annotations[] = AnnotationFactory::createOrFail('@property', '\\' . $className, '$' . $name);
80
        }
81
82
        return $annotations;
83
    }
84
}
85