Application   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 13 5
A getDefaultCommands() 0 9 1
1
<?php
2
3
/*
4
 * This file is part of the php-formatter package
5
 *
6
 * Copyright (c) 2014-2016 Marc Morera
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * Feel free to edit as you please, and have fun.
12
 *
13
 * @author Marc Morera <[email protected]>
14
 */
15
16
namespace Mmoreram\PHPFormatter\Console;
17
18
use Symfony\Component\Console\Application as BaseApplication;
19
20
use Mmoreram\PHPFormatter\Command;
21
22
/**
23
 * Class Application.
24
 */
25
class Application extends BaseApplication
26
{
27
    /**
28
     * Construct method.
29
     */
30
    public function __construct()
31
    {
32
        if (function_exists('ini_set') && extension_loaded('xdebug')) {
33
            ini_set('xdebug.show_exception_trace', false);
34
            ini_set('xdebug.scream', false);
35
        }
36
37
        if (function_exists('date_default_timezone_set') && function_exists('date_default_timezone_get')) {
38
            date_default_timezone_set(@date_default_timezone_get());
39
        }
40
41
        parent::__construct('PHPFormatter');
42
    }
43
44
    /**
45
     * Initializes all the composer commands.
46
     */
47
    protected function getDefaultCommands()
48
    {
49
        $commands = parent::getDefaultCommands();
50
        $commands[] = new Command\UseSortCommand();
51
        $commands[] = new Command\HeaderCommand();
52
        $commands[] = new Command\StrictCommand();
53
54
        return $commands;
0 ignored issues
show
Best Practice introduced by
The expression return $commands; seems to be an array, but some of its elements' types (Mmoreram\PHPFormatter\Co...r\Command\StrictCommand) 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...
55
    }
56
}
57