Completed
Push — master ( b4a6ad...e2bab1 )
by Andrii
13:50
created

VcsignoreController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A actionLoad() 0 7 3
A actionSave() 0 4 1
1
<?php
2
3
/*
4
 * Task runner, code generator and build tool for easier continuos integration
5
 *
6
 * @link      https://github.com/hiqdev/hidev
7
 * @package   hidev
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hidev\controllers;
13
14
/**
15
 * Controller for VCS ignore files generation.
16
 */
17
class VcsignoreController extends FileController
18
{
19
    protected $_items = [
20
        '.hidev/composer.json' => 'hidev internals',
21
        '.hidev/composer.lock' => 'hidev internals',
22
        '.hidev/vendor'        => 'hidev internals',
23
        '.*.swp'               => 'IDE & OS files',
24
        '.idea'                => 'IDE & OS files',
25
        'nbproject'            => 'IDE & OS files',
26
        '.buildpath'           => 'IDE & OS files',
27
        '.project'             => 'IDE & OS files',
28
        '.settings'            => 'IDE & OS files',
29
        'Thumbs.db'            => 'IDE & OS files',
30
        '.DS_Store'            => 'IDE & OS files',
31
    ];
32
33
    public function actionLoad()
34
    {
35
        $items = $this->getFile()->load() ?: [];
36
        if ($items) {
37
            $this->takeVcs()->setIgnore($items);
38
        }
39
    }
40
41
    public function actionSave()
42
    {
43
        return $this->getFile()->save($this->takeVcs()->getIgnore());
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getFile()-...akeVcs()->getIgnore()); (boolean) is incompatible with the return type of the parent method hidev\controllers\FileController::actionSave of type integer.

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...
44
    }
45
}
46