Completed
Push — master ( c4e93d...f0fa26 )
by
unknown
79:49 queued 61:06
created

DebugTemplate::getSourceContext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the DebugTemplate class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Bundle\EzPublishDebugBundle\Twig;
10
11
use Symfony\Component\Filesystem\Filesystem;
12
use Twig\Template;
13
14
/**
15
 * Meant to be used as a Twig base template class.
16
 *
17
 * Wraps the display method to:
18
 * - Inject debug info into template to be able to see in the markup which one is used
19
 */
20
class DebugTemplate extends Template
21
{
22
    private $fileSystem;
23
24
    public function display(array $context, array $blocks = [])
25
    {
26
        $this->fileSystem = $this->fileSystem ?: new Filesystem();
27
28
        // Bufferize to be able to insert template name as HTML comments if applicable.
29
        // Layout template name will only appear at the end, to avoid potential quirks with old browsers
30
        // when comments appear before doctype declaration.
31
        ob_start();
32
        parent::display($context, $blocks);
33
        $templateResult = ob_get_clean();
34
35
        $templateName = trim($this->fileSystem->makePathRelative($this->getSourceContext()->getPath(), dirname(getcwd())), '/');
0 ignored issues
show
Bug introduced by
The method getPath cannot be called on $this->getSourceContext() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
36
        // Check if template name ends with "html.twig", indicating this is an HTML template.
37
        $isHtmlTemplate = substr($templateName, -strlen('html.twig')) === 'html.twig';
38
        $templateName = $isHtmlTemplate ? $templateName . ' (' . $this->getSourceContext()->getName() . ')' : $templateName;
0 ignored issues
show
Bug introduced by
The method getName cannot be called on $this->getSourceContext() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
39
40
        // Display start template comment, if applicable.
41
        if ($isHtmlTemplate) {
42
            if (stripos(trim($templateResult), '<!doctype') !== false) {
43
                $templateResult = preg_replace(
44
                    '#(<!doctype[^>]+>)#im',
45
                    "$1\n<!-- START " . $templateName . ' -->',
46
                    $templateResult
47
                );
48
            } else {
49
                echo "\n<!-- START $templateName -->\n";
50
            }
51
        }
52
53
        // Display stop template comment after result, if applicable.
54
        if ($isHtmlTemplate) {
55
            $bodyPos = stripos($templateResult, '</body>');
56
            if ($bodyPos !== false) {
57
                // Add layout template name before </body>, to avoid display quirks in some browsers.
58
                echo substr($templateResult, 0, $bodyPos)
59
                     . "\n<!-- STOP $templateName -->\n"
60
                     . substr($templateResult, $bodyPos);
61
            } else {
62
                echo $templateResult;
63
                echo "\n<!-- STOP $templateName -->\n";
64
            }
65
        } else {
66
            echo $templateResult;
67
        }
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function getTemplateName()
74
    {
75
        return '';
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function getSourceContext()
82
    {
83
        return '';
0 ignored issues
show
Bug Best Practice introduced by
The return type of return ''; (string) is incompatible with the return type declared by the abstract method Twig\Template::getSourceContext of type Twig\Source.

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...
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    protected function doDisplay(array $context, array $blocks = [])
90
    {
91
        return '';
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function getDebugInfo()
98
    {
99
        return [];
100
    }
101
}
102