Completed
Push — master ( c12891...5636df )
by Martin
02:55
created

FrontMatterLoader::getSource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
/*
4
 * This is part of the webuni/front-matter package.
5
 *
6
 * (c) Martin Hasoň <[email protected]>
7
 * (c) Webuni s.r.o. <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Webuni\FrontMatter\Twig;
14
15
use Webuni\FrontMatter\FrontMatterInterface;
16
17
class FrontMatterLoader implements \Twig_LoaderInterface, \Twig_ExistsLoaderInterface, \Twig_SourceContextLoaderInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Twig_ExistsLoaderInterface has been deprecated with message: since 1.12 (to be removed in 3.0)

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
Deprecated Code introduced by
The interface Twig_SourceContextLoaderInterface has been deprecated with message: since 1.27 (to be removed in 3.0)

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
18
{
19
    private $loader;
20
    private $parser;
21
22
    public function __construct(FrontMatterInterface $parser, \Twig_LoaderInterface $loader)
23
    {
24
        $this->loader = $loader;
25
        $this->parser = $parser;
26
    }
27
28
    public function getSource($name)
29
    {
30
        @trigger_error(sprintf('Calling "getSource" on "%s" is deprecated since Twig 1.27. Use getSourceContext() instead.', get_class($this)), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
31
32
        return $this->getSourceContext($name);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getSourceContext($name); (Twig_Source) is incompatible with the return type declared by the interface Twig_LoaderInterface::getSource 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...
33
    }
34
35
    public function getCacheKey($name)
36
    {
37
        return $this->loader->getCacheKey($name);
38
    }
39
40
    public function isFresh($name, $time)
41
    {
42
        return $this->loader->isFresh($name, $time);
43
    }
44
45
    public function exists($name)
46
    {
47
        if ($this->loader instanceof \Twig_ExistsLoaderInterface) {
48
            return $this->loader->exists($name);
49
        } else {
50
            try {
51
                if ($this->loader instanceof \Twig_SourceContextLoaderInterface) {
52
                    $this->loader->getSourceContext($name);
53
                } else {
54
                    $this->loader->getSource($name);
0 ignored issues
show
Deprecated Code introduced by
The method Twig_LoaderInterface::getSource() has been deprecated with message: since 1.27 (to be removed in 2.0), implement Twig_SourceContextLoaderInterface

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
55
                }
56
57
                return true;
58
            } catch (\Twig_Error_Loader $e) {
59
                return false;
60
            }
61
        }
62
    }
63
64
    public function getSourceContext($name)
65
    {
66
        if ($this->loader instanceof \Twig_SourceContextLoaderInterface) {
67
            $source = $this->loader->getSourceContext($name);
68
        } else {
69
            if ($this->loader instanceof \Twig_ExistsLoaderInterface && !$this->loader->exists($name)) {
70
                throw new \Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
71
            }
72
73
            $source = new \Twig_Source($this->loader->getSource($name), $name);
0 ignored issues
show
Deprecated Code introduced by
The method Twig_LoaderInterface::getSource() has been deprecated with message: since 1.27 (to be removed in 2.0), implement Twig_SourceContextLoaderInterface

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
74
        }
75
76
        return new \Twig_Source($this->parser->parse($source->getCode(), ['filename' => $name]), $source->getName(), $source->getPath());
0 ignored issues
show
Unused Code introduced by
The call to FrontMatterInterface::parse() has too many arguments starting with array('filename' => $name).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
77
    }
78
}
79