NotPhpRenderer   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 6
dl 0
loc 62
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B render() 0 42 5
A addTemplate() 0 5 1
1
<?php
2
/**
3
 * Webino (http://webino.sk)
4
 *
5
 * @link        https://github.com/webino/WebinoDraw for the canonical source repository
6
 * @copyright   Copyright (c) 2012-2017 Webino, s. r. o. (http://webino.sk)
7
 * @author      Peter Bačinský <[email protected]>
8
 * @license     BSD-3-Clause
9
 */
10
11
namespace WebinoDraw\View\Renderer;
12
13
use Zend\View\Exception;
14
use Zend\View\Model\ModelInterface as Model;
15
use Zend\View\Renderer\PhpRenderer;
16
17
/**
18
 * Uses file_get_contents() instead of include
19
 *
20
 * <pre>
21
 * 'service_manager' => [
22
 *     'factories' => [
23
 *         'HttpViewManager' => 'WebinoDraw\Mvc\Service\HttpViewManagerFactory',
24
 *     ],
25
 * ],
26
 * </pre>
27
 */
28
class NotPhpRenderer extends PhpRenderer
29
{
30
    /**
31
     * {@inheritDoc}
32
     */
33
    private $templates = [];
34
35
    /**
36
     * {@inheritDoc}
37
     */
38
    public function render($nameOrModel, $values = null)
39
    {
40
        if ($nameOrModel instanceof Model) {
41
            $model       = $nameOrModel;
42
            $nameOrModel = $model->getTemplate();
43
44
            if (empty($nameOrModel)) {
45
                throw new Exception\DomainException(sprintf(
46
                    '%s: received View Model argument, but template is empty',
47
                    __METHOD__
48
                ));
49
            }
50
51
            // Give view model awareness via ViewModel helper
52
            $this->plugin('view_model')->setCurrent($model);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Zend\View\Helper\AbstractHelper as the method setCurrent() does only exist in the following sub-classes of Zend\View\Helper\AbstractHelper: Zend\View\Helper\ViewModel. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
53
        }
54
55
        // find the script file name using the parent private method
56
        $this->addTemplate($nameOrModel);
57
        unset($nameOrModel);
58
59
        $template = array_pop($this->templates);
60
        $file     = $this->resolver($template);
61
62
        if (!$file) {
63
            throw new Exception\RuntimeException(sprintf(
64
                '%s: Unable to render template "%s"; resolver could not resolve to a file',
65
                __METHOD__,
66
                $template
67
            ));
68
        }
69
70
        if (false === is_file($file)) {
71
            throw new Exception\UnexpectedValueException(sprintf(
72
                '%s: Unable to render template "%s"; file not found',
73
                __METHOD__,
74
                $file
75
            ));
76
        }
77
78
        return $this->getFilterChain()->filter(file_get_contents($file));
79
    }
80
81
    /**
82
     * {@inheritDoc}
83
     */
84
    public function addTemplate($template)
85
    {
86
        $this->templates[] = $template;
87
        return $this;
88
    }
89
}
90