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

LanguageConstruct::compile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
/******************************************************************************
3
 * An implementation of dicto (scg.unibe.ch/dicto) in and for PHP.
4
 *
5
 * Copyright (c) 2016 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\PredicateFactory;
14
15
// TODO: Maybe this should not extend Entities
16
class LanguageConstruct extends Entities {
17
    /**
18
     * @var string
19
     */
20
    private $construct_name;
21
22 40
    public function __construct($construct_name, $name = null) {
23 40
        parent::__construct($name);
24 40
        assert('is_string($construct_name)');
25
        // TODO: Restrict the possible construct_names (like @, unset, echo, ...)
0 ignored issues
show
Unused Code Comprehensibility introduced by
36% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
26 40
        $this->construct_name = $construct_name;     
27 40
    }
28
29
    /**
30
     * @return  string
31
     */
32 112
    public function construct_name() {
33 112
        return $this->construct_name;
34
    }
35
36
    /**
37
     * @inheritdoc
38
     */
39 108
    public function meaning() {
40 108
        return $this->construct_name();
41
    }
42
43
    /**
44
     * @inheritdoc
45
     */
46 3
    public function id() {
47 3
        return Variable::LANGUAGE_CONSTRUCT_TYPE;
48
    }
49
50
    /**
51
     * @inheritdocs
52
     */
53 4
    public function compile(PredicateFactory $f) {
54 4
        return $f->_and
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $f->_and(array($f...s->construct_name()))); (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...
55 4
            ([$f->_type_is(Variable::LANGUAGE_CONSTRUCT_TYPE)
56
            // TODO: property->equals would help
57 4
            , $f->_property("name")->_matches($this->construct_name())
58 4
            ]);
59
    }
60
}
61
62