Completed
Pull Request — master (#33)
by Daniel
15:07 queued 09:14
created

TwigRenderer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 42
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B render() 0 29 4
1
<?php
2
3
namespace Psi\Bridge\ContentType\Twig;
4
5
use Psi\Component\ContentType\View\RendererInterface;
6
use Psi\Component\ContentType\View\View;
7
8
class TwigRenderer implements RendererInterface
9
{
10
    private $twig;
11
12
    public function __construct(\Twig_Environment $twig)
13
    {
14
        $this->twig = $twig;
15
    }
16
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function render(View $view)
21
    {
22
        $templateName = $view->getTemplate();
23
        $names = [
24
            $templateName,
25
            sprintf('%s.html.twig', $templateName),
26
            sprintf('%s.twig', $templateName),
27
        ];
28
29
        foreach ($names as $name) {
30
            try {
31
                $template = $this->twig->loadTemplate($name);
32
                break;
33
            } catch (\Twig_Error_Loader $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
34
            }
35
        }
36
37
        if (!$template) {
0 ignored issues
show
Bug introduced by
The variable $template does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
38
            throw new \InvalidArgumentException(sprintf(
39
                'Could not load template "%s", tried: "%s"',
40
                $templateName, implode('", "', $names)
41
            ));
42
        }
43
44
        return $template->render([
45
            'vars' => $view->getVars(),
46
            'value' => $view->getValue(),
47
        ]);
48
    }
49
}
50