Completed
Push — master ( 3c5828...866487 )
by Dorian
02:10
created

AppConsole   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 21.43 %

Coupling/Cohesion

Components 1
Dependencies 2
Metric Value
wmc 4
lcom 1
cbo 2
dl 9
loc 42
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultCommands() 0 9 1
A getApplicationRootDir() 0 4 1
A getRootDir() 9 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Application;
4
5
use Symfony\Component\Console\Application;
6
7
use Application\Commands\CodingStandardCommand;
8
9
/**
10
 * Application Console
11
 */
12
class AppConsole extends Application
13
{
14
    /** @var string */
15
    protected $rootDir;
16
17
    /**
18
     * {@inheritDoc}
19
     */
20
    protected function getDefaultCommands()
21
    {
22
        return array_merge(
0 ignored issues
show
Best Practice introduced by
The expression return array_merge(paren...ingStandardCommand())); seems to be an array, but some of its elements' types (Application\Commands\CodingStandardCommand) are incompatible with the return type of the parent method Symfony\Component\Consol...ion::getDefaultCommands of type array<Symfony\Component\...le\Command\ListCommand>.

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 new Author('Johannes');
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return '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...
23
            parent::getDefaultCommands(),
24
            array(
25
                new CodingStandardCommand(),
26
            )
27
        );
28
    }
29
30
    /**
31
     * @return string
32
     */
33
    public function getApplicationRootDir()
34
    {
35
        return realpath($this->getRootDir().'/..');
36
    }
37
38
    /**
39
     * @return string
40
     *
41
     * @see \Symfony\Component\HttpKernel\Kernel::getRootDir()
42
     * @author Fabien Potencier <[email protected]>
43
     */
44 View Code Duplication
    private function getRootDir()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
    {
46
        if (null === $this->rootDir) {
47
            $r = new \ReflectionObject($this);
48
            $this->rootDir = str_replace('\\', '/', dirname($r->getFileName()));
49
        }
50
51
        return $this->rootDir;
52
    }
53
}
54