Completed
Pull Request — master (#36)
by Daniel
04:59
created

PhpRenderer::render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 2
eloc 13
nc 2
nop 1
1
<?php
2
3
namespace Psi\Component\ContentType\View;
4
5
use Psi\Component\ContentType\View\ViewInterface;
6
use Psi\Component\ContentType\View\RendererInterface;
7
8
class PhpRenderer implements RendererInterface
9
{
10
    private $templatePath;
11
12
    public function __construct(string $templatePath)
13
    {
14
        $this->templateDir = $templatePath;
0 ignored issues
show
Bug introduced by
The property templateDir does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
15
    }
16
17
    public function render(ViewInterface $view)
18
    {
19
        $templateName = $view->getTemplate();
20
        $templatePath = sprintf('%s/%s.php', $this->templatePath, $template);
0 ignored issues
show
Bug introduced by
The variable $template does not exist. Did you mean $templateName?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
21
22
        if (!file_exists($template)) {
0 ignored issues
show
Bug introduced by
The variable $template does not exist. Did you mean $templateName?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
23
            throw new TemplateNotFoundException(sprintf(
24
                'Template "%s" not found at "%s"',
25
                $templateName, $templatePath
26
            ));
27
        }
28
29
        $output = function () use ($view) {
30
            ob_start();
31
            $renderer = $this;
0 ignored issues
show
Unused Code introduced by
$renderer is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
32
            require_once($template);
0 ignored issues
show
Bug introduced by
The variable $template does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
33
            return ob_get_clean();
34
        };
35
36
        return $output;
37
    }
38
}
39