Renderer::render()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 37
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 19
nc 5
nop 4
dl 0
loc 37
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace WebinoDomLib\Dom;
4
5
use WebinoDomLib\Dom;
6
use WebinoDomLib\Event\RenderEvent;
7
use WebinoEventLib\EventsAwareInterface;
8
use WebinoEventLib\EventsAwareTrait;
9
10
/**
11
 * Class Renderer
12
 */
13
class Renderer implements EventsAwareInterface
14
{
15
    use EventsAwareTrait;
16
17
    /**
18
     * @param NodeLocatorInterface $doc
19
     * @param State $cfg
20
     */
21
    public function render(NodeLocatorInterface $doc, Config $cfg, RenderEvent $parentEvent = null, callable $callback = null)
22
    {
23
        /** @var Config\Spec $spec */
24
        foreach ($cfg->getQueue() as $spec) {
25
            // TODO skip if locator or node owner document empty
26
27
            $events = $this->getEvents();
28
            $nodes  = $doc->locate($spec->getLocator());
29
30
            $callback and call_user_func($callback, $spec, $nodes);
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
31
32
            /** @var NodeInterface $node */
33
            foreach ($nodes as $node) {
34
35
                $event = new RenderEvent($this, $node, $spec, $parentEvent);
0 ignored issues
show
Bug introduced by
It seems like $parentEvent defined by parameter $parentEvent on line 21 can also be of type object<WebinoDomLib\Event\RenderEvent>; however, WebinoDomLib\Event\RenderEvent::__construct() does only seem to accept null|object<self>, 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...
36
37
                $events->attach(RenderEvent::class, function (RenderEvent $subEvent) use ($spec, $event) {
38
                    $view = $spec->getView();
39
                    if (empty($view)) {
40
                        return;
41
                    }
42
43
                    $this->render(
44
                        $subEvent->getNode(),
45
                        new Config($view),
46
                        $event,
47
                        function(Config\Spec $spec, NodeList $nodes) use ($subEvent) {
48
                            $subEvent->setNode($nodes, $spec->getKey());
0 ignored issues
show
Documentation introduced by
$nodes is of type object<WebinoDomLib\Dom\NodeList>, but the function expects a object<WebinoDomLib\Dom\NodeInterface>.

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...
49
                        }
50
                    );
51
                }, RenderEvent::FINISH);
52
53
54
                $events->trigger($event);
55
            }
56
        }
57
    }
58
}
59