HeaderFixer::fix()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
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\Fixer;
17
18
use Mmoreram\PHPFormatter\Fixer\Interfaces\FixerInterface;
19
20
/**
21
 * Class HeaderFixer.
22
 */
23
class HeaderFixer implements FixerInterface
24
{
25
    /**
26
     * @var string
27
     *
28
     * Header
29
     */
30
    protected $header;
31
32
    /**
33
     * Construct method.
34
     *
35
     * @param string $header Header
36
     */
37
    public function __construct($header)
38
    {
39
        $this->header = '<?php' . rtrim("\n\n" . trim($header) . "\n\n") . "\n\n";
40
    }
41
42
    /**
43
     * Fix any piece of code given as parameter.
44
     *
45
     * @param string $data Data
46
     *
47
     * @return string Data fixed
48
     */
49
    public function fix($data)
50
    {
51
        $regex = '~(?P<group>^\s*<\?php(?:(?:(?:/\*.*?\*/)|(?:(?://|#).*?\n{1})|(?:\s*))*))(?P<other>.*)~s';
52
        preg_match($regex, $data, $results);
53
54
        if (!isset($results['group'])) {
55
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface Mmoreram\PHPFormatter\Fi...ces\FixerInterface::fix of type string.

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...
56
        }
57
58
        $other = $results['other'];
59
60
        return $this->header . ltrim($other);
61
    }
62
}
63