Completed
Push — master ( 1e9ef5...3740bc )
by Richard
05:53 queued 29s
created

WithProperty   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 93.55%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 70
ccs 29
cts 31
cp 0.9355
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A variable() 0 3 1
A meaning() 0 3 1
B argument_list() 0 21 5
A compile() 0 6 1
1
<?php
2
/******************************************************************************
3
 * An implementation of dicto (scg.unibe.ch/dicto) in and for PHP.
4
 * 
5
 * Copyright (c) 2016, 2015 Richard Klees <[email protected]>
6
 *
7
 * This software is licensed under The MIT License. You should have received 
8
 * a copy of the license along with the code.
9
 */
10
11
namespace Lechimp\Dicto\Variables;
12
13
use Lechimp\Dicto\Graph\Node;
14
use Lechimp\Dicto\Graph\PredicateFactory;
15
16
/**
17
 * A variable with a certain property.
18
 */
19
class WithProperty extends Variable {
20
    /**
21
     * @var Variable
22
     */
23
    private $other;
24
25
    /**
26
     * @var Property
27
     */
28
    private $property;
29
30
    /**
31
     * @var array
32
     */
33
    private $arguments;
34
35 29
    public function __construct(Variable $other, Property $property, array $arguments) {
36 29
        parent::__construct();
37 29
        assert('$property->arguments_are_valid($arguments)');
38 29
        $this->other = $other;
39 29
        $this->property = $property;
40 29
        $this->arguments = $arguments;
41 29
    }
42
43
    /**
44
     * @return  Variable
45
     */
46 14
    public function variable() {
47 14
        return $this->other;
48
    }
49
50
    /**
51
     * @inheritdocs
52
     */
53 14
    public function meaning() {
54 14
        return $this->variable()->meaning()." ".$this->property->parse_as().": ".$this->argument_list();
55
    }
56
57 14
    protected function argument_list() {
58 14
        $args = array();
59 14
        foreach ($this->arguments as $argument) {
60 14
            if (is_string($argument)) {
61 12
                $args[] = "\"$argument\"";
62 12
            }
63 2
            else if ($argument instanceof Variable) {
64 2
                $name = $argument->name();
65 2
                if ($name !== null) {
66 2
                    $args[] = $name;
67 2
                }
68
                else {
69
                    $args[] = "(".$argument->meaning().")";
70
                }
71 2
            }
72
            else {
73
                throw new \LogicException("Unknown arg type: ".gettype($argument));
74
            }
75 14
        }
76 14
        return implode(", ", $args);
77
    }
78
79
    /**
80
     * @inheritdocs
81
     */
82 23
    public function compile(PredicateFactory $f) {
83 23
        $l = $this->other->compile($f);
84 23
        $p = $this->property->compile($f, $this->arguments);
85
86 23
        return $f->_and([$l,$p]);
0 ignored issues
show
Documentation introduced by
array($l, $p) is of type array<integer,object<Lec...o\\Graph\\Predicate>"}>, but the function expects a array<integer,object<Lec...Dicto\Graph\Predicate>>.

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...
Bug Best Practice introduced by
The return type of return $f->_and(array($l, $p)); (Lechimp\Dicto\Graph\Predicate\_And) is incompatible with the return type declared by the abstract method Lechimp\Dicto\Variables\Variable::compile of type Lechimp\Dicto\Graph\PredicateFactory.

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...
87
    }
88
}
89