Completed
Push — develop ( e653e2...0197dd )
by
unknown
06:51
created

Proxy   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
c 2
b 0
f 0
lcom 0
cbo 3
dl 0
loc 75
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 14 3
B plugin() 0 24 5
A exists() 0 4 1
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2017 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace Core\View\Helper;
12
13
use Core\View\Helper\Proxy\HelperProxy;
14
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
15
use Zend\ServiceManager\Exception\ServiceNotFoundException;
16
use Zend\View\Helper\AbstractHelper;
17
18
/**
19
 * View helper to safely use module specific view helpers in other modules.
20
 *
21
 * In most scenarios, to make something failsafe, it takes a lot of (redundant) code.
22
 * With this proxy helper, this can be simplified by just adding a few characters.
23
 *
24
 * <pre>
25
 *
26
 *      //
27
 *      // ORIGINAL
28
 *      //
29
 *      <?=$this->helper()->someOutput()?>
30
 *
31
 *      //
32
 *      // Failsafe code
33
 *      //
34
 *      <? if ($this->getHelperPluginManager()->has('plugin')):
35
 *              $plugin = $this->getHelperPluginManager()->get('helper');
36
 *
37
 *              // do something with the plugin, i.E. call some method
38
 *              echo $plugin->someOutput();
39
 *         else:
40
 *              // helper does not exist.
41
 *              // do something to hanlde this case.
42
 *              echo '';
43
 *         endif;
44
 *      ?>
45
 *
46
 *      //
47
 *      // Failsafe with Proxy:
48
 *      //
49
 *      <?=$this->proxy('helper')->someOutput()?>
50
 *
51
 *      // If 'helper' does not exist, a ProxyNoopHelper is returned, which
52
 *      // simply returns NULL on any method call and whose string representation is
53
 *      // an empty string.
54
 * <pre>
55
 *
56
 * Advanced usage:
57
 *
58
 * - Load a helper without risking an exception:
59
 *   This will return a {@link \Core\View\Helper\Proxy\HelperProxy} instance.
60
 *   <pre><? $helper = $this->proxy()->plugin('HelperServiceName', [options])?></pre>
61
 *
62
 * - Call an invokable helper and expect an array returned.
63
 *   This will return an empty array, if the helper does not exist.
64
 *   <pre><? $array = $this->proxy('helper', Proxy::EXPECT_ARRAY)?></pre>
65
 *
66
 * - Expect other return types:
67
 *   <pre>
68
 *      // Object (which is default)
69
 *      $helper = $this->proxy('helper', Proxy::EXPECT_OBJECT);
70
 *
71
 *      // Iterator (returns an IteratorAggregate which returns an empty ArrayIterator.
72
 *      $helper = $this->proxy('helper', Proxy::EXPECT_ITERATOR);
73
 *
74
 *      // Skalar values: Simply specify the expected result, which will be returned,
75
 *      // if the helper does not exist.
76
 *      $value = $this->proxy('helper', ''); // return empty string.
77
 *      $value = $this->proxy('helper', false) // return FALSE
78
 *   </pre>
79
 *
80
 * - Directly calls an invokable helper with Arguments
81
 *   <pre>
82
 *      <?=$this->proxy('invokableHelper', ['arg1', 'arg2'], false)?>
83
 *   </pre>
84
 *
85
 * - Call a method on a plugin
86
 *   (see {@link HelperProxy::call()})
87
 *   <pre>
88
 *      $this->proxy('helper')->call('method', ['arg1',...], 'expectedValue');
89
 *   </pre>
90
 *
91
 * - Call consecutive methods on plugin
92
 *   (see {@link HelperProxy::consecutive()})
93
 *   <pre>
94
 *      $this->proxy('helper')->consecutive(['method1', 'method2' => ['arg1', ... ], ...]);
95
 *   </pre>
96
 *
97
 * - Call chained methods on plugin
98
 *   (see {@link HelperProxy::chain()})
99
 *   <pre>
100
 *      $this->proxy('helper')->chain(['method' => ['arg'], 'method2'], 'expectedValue');
101
 *   </pre>
102
 *
103
 * @author Mathias Gelhausen <[email protected]>
104
 * @since 0.29
105
 */
106
class Proxy extends AbstractHelper
107
{
108
    /**
109
     * Loads and possiblity executes a view helper.
110
     *
111
     * @param null|string   $plugin
112
     * @param array|string  $args
0 ignored issues
show
Documentation introduced by
Should the type for parameter $args not be array|string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
113
     * @param mixed         $expect
114
     *
115
     * @return mixed|self|ProxyNoopHelper|ProxyNoopIterator
116
     */
117
    public function __invoke($plugin = null, $args = null, $expect = HelperProxy::EXPECT_SELF)
118
    {
119
        if (null === $plugin) {
120
            return $this;
121
        }
122
123
        $plugin = $this->plugin($plugin);
124
125
        if (null === $args) {
126
            return $plugin;
127
        }
128
129
        return $plugin->call('__invoke', $args, $expect);
130
    }
131
132
    /**
133
     * Loads a plugin from the plugin helper manager.
134
     *
135
     * Returns false, if either the helper plugin manager cannot be
136
     * retrieved from the renderer or the requested plugin does not exist.
137
     *
138
     * @param string $plugin
139
     * @param true|array  $options if true, only return if plugin exists or not.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $options not be true|array|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
140
     *
141
     * @return bool|object
142
     */
143
    public function plugin($plugin, $options = null)
144
    {
145
        $renderer = $this->getView();
146
147
        if (!method_exists($renderer, 'getHelperPluginManager')) {
148
            return true === $options ? false : new HelperProxy(false);
149
        }
150
151
        /* @var \Zend\View\HelperPluginManager $manager */
152
        $manager   = $renderer->getHelperPluginManager();
153
        $hasPlugin = $manager->has($plugin);
154
155
        if (true === $options) {
156
            return $hasPlugin;
157
        }
158
159
        if ($hasPlugin) {
160
            $pluginInstance = $manager->get($plugin, $options);
0 ignored issues
show
Bug introduced by
It seems like $options defined by parameter $options on line 143 can also be of type null; however, Zend\ServiceManager\AbstractPluginManager::get() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
161
        } else {
162
            $pluginInstance = false;
163
        }
164
165
        return new HelperProxy($pluginInstance);
0 ignored issues
show
Bug introduced by
It seems like $pluginInstance defined by $manager->get($plugin, $options) on line 160 can also be of type array or object; however, Core\View\Helper\Proxy\HelperProxy::__construct() does only seem to accept false|object<Zend\View\Helper\AbstractHelper>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
166
    }
167
168
    /**
169
     * Does a plugin exists?
170
     *
171
     * @param string $plugin
172
     *
173
     * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be HelperProxy|boolean?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
174
     */
175
    public function exists($plugin)
176
    {
177
        return $this->plugin($plugin, true);
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a object<Core\View\Helper\true>|array|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
178
    }
179
180
}
181