GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ProcedureCompiler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
ccs 7
cts 7
cp 1
rs 9.6666
cc 1
eloc 7
nc 1
nop 4
crap 1
1
<?php
2
3
/*
4
 * This file is part of the php-phantomjs.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace JonnyW\PhantomJs\Procedure;
11
12
use JonnyW\PhantomJs\Cache\CacheInterface;
13
use JonnyW\PhantomJs\Template\TemplateRendererInterface;
14
15
/**
16
 * PHP PhantomJs
17
 *
18
 * @author Jon Wenmoth <[email protected]>
19
 */
20
class ProcedureCompiler implements ProcedureCompilerInterface
21
{
22
    /**
23
     * Procedure loader
24
     *
25
     * @var \JonnyW\PhantomJs\Procedure\ProcedureLoaderInterface
26
     * @access protected
27
     */
28
    protected $procedureLoader;
29
30
    /**
31
     * Procedure validator
32
     *
33
     * @var \JonnyW\PhantomJs\Procedure\ProcedureValidatorInterface
34
     * @access protected
35
     */
36
    protected $procedureValidator;
37
38
    /**
39
     * Cache handler
40
     *
41
     * @var \JonnyW\PhantomJs\Cache\CacheInterface
42
     * @access protected
43
     */
44
    protected $cacheHandler;
45
46
    /**
47
     * Renderer
48
     *
49
     * @var \JonnyW\PhantomJs\Template\TemplateRendererInterface
50
     * @access protected
51
     */
52
    protected $renderer;
53
54
    /**
55
     * Cache enabled
56
     *
57
     * @var boolean
58
     * @access protected
59
     */
60
    protected $cacheEnabled;
61
62
    /**
63
     * Internal constructor
64
     *
65
     * @access public
66
     * @param \JonnyW\PhantomJs\Procedure\ProcedureLoaderInterface    $procedureLoader
67
     * @param \JonnyW\PhantomJs\Procedure\ProcedureValidatorInterface $procedureValidator
68
     * @param \JonnyW\PhantomJs\Cache\CacheInterface                  $cacheHandler
69
     * @param \JonnyW\PhantomJs\Template\TemplateRendererInterface    $renderer
70
     */
71 6
    public function __construct(ProcedureLoaderInterface $procedureLoader, ProcedureValidatorInterface $procedureValidator,
72
        CacheInterface $cacheHandler, TemplateRendererInterface $renderer)
73
    {
74 6
        $this->procedureLoader    = $procedureLoader;
75 6
        $this->procedureValidator = $procedureValidator;
76 6
        $this->cacheHandler       = $cacheHandler;
77 6
        $this->renderer           = $renderer;
78 6
        $this->cacheEnabled       = true;
79 6
    }
80
81
    /**
82
     * Compile partials into procedure.
83
     *
84
     * @access public
85
     * @param  \JonnyW\PhantomJs\Procedure\ProcedureInterface $procedure
86
     * @param  \JonnyW\PhantomJs\Procedure\InputInterface     $input
87
     * @return void
88
     */
89 42
    public function compile(ProcedureInterface $procedure, InputInterface $input)
90
    {
91 42
        $cacheKey = sprintf('phantomjs_%s_%s', $input->getType(), md5($procedure->getTemplate()));
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface JonnyW\PhantomJs\Procedure\InputInterface as the method getType() does only exist in the following implementations of said interface: JonnyW\PhantomJs\Http\AbstractRequest, JonnyW\PhantomJs\Http\CaptureRequest, JonnyW\PhantomJs\Http\PdfRequest, JonnyW\PhantomJs\Http\Request.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
92
93 42
        if ($this->cacheEnabled && $this->cacheHandler->exists($cacheKey)) {
94 31
            $template = $this->cacheHandler->fetch($cacheKey);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $template is correct as $this->cacheHandler->fetch($cacheKey) (which targets JonnyW\PhantomJs\Cache\CacheInterface::fetch()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
95 31
        }
96
97 42
        if (empty($template)) {
98
99 12
            $template  = $this->renderer
100 12
                ->render($procedure->getTemplate(), array('engine' => $this, 'procedure_type' => $input->getType()));
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface JonnyW\PhantomJs\Procedure\InputInterface as the method getType() does only exist in the following implementations of said interface: JonnyW\PhantomJs\Http\AbstractRequest, JonnyW\PhantomJs\Http\CaptureRequest, JonnyW\PhantomJs\Http\PdfRequest, JonnyW\PhantomJs\Http\Request.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
101
102 12
            $test = clone $procedure;
103 12
            $test->setTemplate($template);
104
105 12
            $compiled = $test->compile($input);
106
107 12
            $this->procedureValidator->validate($compiled);
108
109 10
            if ($this->cacheEnabled) {
110 9
                $this->cacheHandler->save($cacheKey, $template);
111 9
            }
112 10
        }
113
114 40
        $procedure->setTemplate($template);
115 40
    }
116
117
    /**
118
     * Load partial template.
119
     *
120
     * @access public
121
     * @param  string $name
122
     * @return string
123
     */
124 5
    public function load($name)
125
    {
126 5
        return $this->procedureLoader->loadTemplate($name, 'partial');
127
    }
128
129
    /**
130
     * Enable cache.
131
     *
132
     * @access public
133
     * @return void
134
     */
135 1
    public function enableCache()
136
    {
137 1
        $this->cacheEnabled = true;
138 1
    }
139
140
    /**
141
     * Disable cache.
142
     *
143
     * @access public
144
     * @return void
145
     */
146 1
    public function disableCache()
147
    {
148 1
        $this->cacheEnabled = false;
149 1
    }
150
151
    /**
152
     * Clear cache.
153
     *
154
     * @access public
155
     * @return void
156
     */
157 1
    public function clearCache()
158
    {
159 1
        $this->cacheHandler->delete('phantomjs_*');
160 1
    }
161
}
162