Completed
Push — master ( c5bd76...a04aa9 )
by Ivannis Suárez
04:38
created

Selectors   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 115
wmc 12
lcom 2
cbo 4
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A factory() 0 8 2
A setFactory() 0 4 1
A addSelector() 0 4 1
A addNamespace() 0 4 1
A __construct() 0 4 1
A __callStatic() 0 4 1
A __call() 0 4 1
A apply() 0 4 1
A select() 0 6 1
A acceptSelectorVisitor() 0 4 1
A selector() 0 4 1
1
<?php
2
3
/**
4
 * This file is part of the Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Cubiche\Core\Selector;
12
13
/**
14
 * Selector Builder Class.
15
 *
16
 * @method static Selectors key(string $name)
17
 * @method static Selectors property(string $name)
18
 * @method static Selectors method(string $name)
19
 * @method static Selectors custom(callable $callable)
20
 * @method static Selectors value($value)
21
 * @method static Selectors count()
22
 * @method static Selectors composite(SelectorInterface $x, SelectorInterface $y)
23
 * @method static Selectors this()
24
 *
25
 * @author Karel Osorio Ramírez <[email protected]>
26
 */
27
class Selectors extends Selector
28
{
29
    /**
30
     * @var SelectorFactoryInterface
31
     */
32
    protected static $factory;
33
34
    /**
35
     * @var SelectorInterface
36
     */
37
    private $selector;
38
39
    /**
40
     * @return \Cubiche\Core\Selector\SelectorFactoryInterface
41
     */
42
    protected static function factory()
43
    {
44
        if (self::$factory === null) {
45
            self::setFactory(new SelectorFactory(__NAMESPACE__));
46
        }
47
48
        return self::$factory;
49
    }
50
51
    /**
52
     * @param SelectorFactoryInterface $factory
53
     */
54
    public static function setFactory(SelectorFactoryInterface $factory)
55
    {
56
        self::$factory = $factory;
57
    }
58
59
    /**
60
     * @param string $selectorClass
61
     * @param string $selectorName
62
     */
63
    public static function addSelector($selectorClass, $selectorName = null)
64
    {
65
        self::factory()->addSelector($selectorClass, $selectorName);
66
    }
67
68
    /**
69
     * @param string $namespace
70
     */
71
    public static function addNamespace($namespace)
72
    {
73
        self::factory()->addNamespace($namespace);
74
    }
75
76
    /**
77
     * @param SelectorInterface $selector
78
     */
79
    protected function __construct(SelectorInterface $selector)
80
    {
81
        $this->selector = $selector;
82
    }
83
84
    /**
85
     * @param string $method
86
     * @param array  $arguments
87
     *
88
     * @return mixed
89
     */
90
    public static function __callStatic($method, $arguments)
91
    {
92
        return new static(self::factory()->create($method, $arguments));
93
    }
94
95
    /**
96
     * @param string $method
97
     * @param array  $args
0 ignored issues
show
Bug introduced by
There is no parameter named $args. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
98
     *
99
     * @return mixed
100
     */
101
    public function __call($method, $arguments)
102
    {
103
        return $this->select(self::factory()->create($method, $arguments));
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function apply($value)
110
    {
111
        return $this->selector()->apply($value);
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function select(SelectorInterface $selector)
118
    {
119
        $this->selector = $this->selector()->select($selector);
120
121
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Cubiche\Core\Selector\Selectors) is incompatible with the return type of the parent method Cubiche\Core\Selector\Selector::select of type Cubiche\Core\Selector\Composite.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
122
    }
123
124
    /**
125
     * @param SelectorVisitorInterface $visitor
126
     *
127
     * @return mixed
128
     */
129
    public function acceptSelectorVisitor(SelectorVisitorInterface $visitor)
130
    {
131
        return $this->selector()->acceptSelectorVisitor($visitor);
132
    }
133
134
    /**
135
     * @return \Cubiche\Core\Selector\SelectorInterface
136
     */
137
    public function selector()
138
    {
139
        return $this->selector;
140
    }
141
}
142