Completed
Pull Request — master (#1)
by Greg
02:18
created

TestBoot   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 76
wmc 10
lcom 1
cbo 0
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A isValid() 0 4 1
A getBootstrap() 0 10 2
A getPath() 0 4 1
A getSiteSelector() 0 4 1
A getFrameworkVersion() 0 4 1
A getServiceFeatures() 0 4 1
A boot() 0 4 1
A terminate() 0 3 1
1
<?php
2
namespace Consolidation\TestUtils;
3
4
use Consolidation\Bootstrap\BootInterface;
5
use Consolidation\Bootstrap\BootstrapSelectionInterface;
6
7
class TestBoot implements BootInterface, BootstrapSelectionInterface
8
{
9
    protected $path;
10
    protected $siteSelector;
11
    protected $version;
12
13
    public function __construct($path, $version, $services)
14
    {
15
        $this->path = $path;
16
        $this->version = $version;
17
        $this->services = $services;
0 ignored issues
show
Bug introduced by
The property services does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
18
    }
19
20
    /**
21
     * @inheritdoc
22
     */
23
    function isValid($path)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
24
    {
25
        return $this->path == $path;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->path == $path; (boolean) is incompatible with the return type declared by the interface Consolidation\Bootstrap\...ctionInterface::isValid of type Consolidation\Bootstrap\TRUE.

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...
26
    }
27
28
    /**
29
     * @inheritdoc
30
     */
31
    function getBootstrap($path, $siteSelector = null)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
32
    {
33
        if (!$this->isValid($path)) {
34
            throw new \RuntimeException('Invalid path passed to getBootstrap().');
35
        }
36
        $this->siteSelection = $siteSelector;
0 ignored issues
show
Bug introduced by
The property siteSelection does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
37
        // Normally we would instantiate a new BootInterface and pass
38
        // the $path to its constructor.
39
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Consolidation\TestUtils\TestBoot) is incompatible with the return type declared by the interface Consolidation\Bootstrap\...Interface::getBootstrap of type Consolidation\Bootstrap\...Bootstrap\BootInterface.

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...
40
    }
41
42
    function getPath()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
43
    {
44
        return $this->path;
45
    }
46
47
    function getSiteSelector()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
48
    {
49
        return $this->siteSelection;
50
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55
    function getFrameworkVersion()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
56
    {
57
        return $this->version;
58
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63
    function getServiceFeatures()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
64
    {
65
        return $this->services();
0 ignored issues
show
Bug introduced by
The method services() does not seem to exist on object<Consolidation\TestUtils\TestBoot>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
66
    }
67
68
    /**
69
     * @inheritdoc
70
     */
71
    public function boot($serviceFeatures, $siteSelector = null)
72
    {
73
        return true;
74
    }
75
76
    /**
77
     * @inheritdoc
78
     */
79
    public function terminate()
80
    {
81
    }
82
}
83