FilesystemTask   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
cbo 2
dl 0
loc 41
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A configure() 0 4 1
A getName() 0 4 1
A resolveFiles() 0 4 1
1
<?php
2
3
/**
4
 * This file is part of Bldr.io
5
 *
6
 * (c) Aaron Scherer <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE
10
 */
11
12
namespace Bldr\Block\Filesystem\Task;
13
14
use Bldr\Block\Core\Task\AbstractTask;
15
use Symfony\Component\Filesystem\Filesystem;
16
17
/**
18
 * @author Aaron Scherer <[email protected]>
19
 */
20
abstract class FilesystemTask extends AbstractTask
21
{
22
    /**
23
     * @var Filesystem $fileSystem
24
     */
25
    protected $fileSystem;
26
27
    /**
28
     * Builds call with an instance of Symfony's Filesystem component
29
     */
30
    public function __construct()
31
    {
32
        $this->fileSystem = new Filesystem();
33
    }
34
35
    /**
36
     * {@inheritDoc}
37
     */
38
    public function configure()
39
    {
40
        $this->addParameter('files', true, "Files to run the filesystem command on");
41
    }
42
43
    /**
44
     * {@inheritDoc}
45
     */
46
    public function getName()
47
    {
48
        return 'filesystem:'.parent::getName();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return 'filesystem:' . parent::getName(); (string) is incompatible with the return type declared by the interface Bldr\Task\TaskInterface::getName of type Bldr\Task\TaskInterface.

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...
49
    }
50
51
    /**
52
     * Returns an array of files or directories for the call
53
     *
54
     * @return array
55
     */
56
    protected function resolveFiles()
57
    {
58
        return $this->getParameter('files');
59
    }
60
}
61