ElementWrapper::__invoke()   B
last analyzed

Complexity

Conditions 10
Paths 20

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 7.6666
c 0
b 0
f 0
cc 10
nc 20
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Draw\LoopHelper;
12
13
use ArrayObject;
14
use WebinoDraw\Exception\InvalidLoopHelperOptionException;
15
16
/**
17
 *
18
 */
19
class ElementWrapper implements HelperInterface
20
{
21
    /**
22
     * {@inheritDoc}
23
     * @throws InvalidLoopHelperOptionException
24
     */
25
    public function __invoke(ArrayObject $loopArgument, array $options)
26
    {
27
        if (empty($options['elementName'])) {
28
            throw new InvalidLoopHelperOptionException('elementName', $loopArgument['spec']);
29
        }
30
        if (!empty($options['elementAttribs']) && !is_array($options['elementAttribs'])) {
31
            throw new InvalidLoopHelperOptionException('elementAttribs', $loopArgument['spec']);
32
        }
33
        if (empty($options['each'])) {
34
            throw new InvalidLoopHelperOptionException('each', $loopArgument['spec']);
35
        }
36
        if (($loopArgument['index'] - 1) % $options['each']) {
37
            return;
38
        }
39
40
        // create wrapper
41
        $newParentNode = $loopArgument['parentNode']->ownerDocument
42
            ->createElement($options['elementName']);
43
44
        $parentNode = !empty($loopArgument['wrapperParentNode'])
45
                    ? $loopArgument['wrapperParentNode']
46
                    : $loopArgument['wrapperParentNode'] = $loopArgument['parentNode'];
47
48
        $beforeNode = !empty($loopArgument['wrapperBeforeNode'])
49
                    ? $loopArgument['wrapperBeforeNode']
50
                    : $loopArgument['wrapperBeforeNode'] = $loopArgument['beforeNode'];
51
52
        $loopArgument['beforeNode'] = null;
53
        $loopArgument['parentNode'] = $beforeNode
54
                                    ? $parentNode->insertBefore($newParentNode, $beforeNode)
55
                                    : $parentNode->appendChild($newParentNode);
56
57
        empty($options['elementAttribs']) or
58
            $newParentNode->setAttributes($options['elementAttribs']);
59
    }
60
}
61