Locator::normalizeLocator()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 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
 * @license     BSD-3-Clause
8
 */
9
10
namespace WebinoDraw\Dom;
11
12
use ArrayObject;
13
use WebinoDraw\Exception\InvalidArgumentException;
14
use WebinoDraw\Exception\UnexpectedValueException;
15
use WebinoDraw\Dom\Locator\Transformator;
16
17
/**
18
 * Class Locator
19
 */
20
class Locator extends ArrayObject
21
{
22
    /**
23
     * @var Transformator
24
     */
25
    protected $transformator;
26
27
    /**
28
     * @param Transformator $transformator
29
     * @param string|array $input
30
     */
31
    public function __construct(Transformator $transformator, $input = null)
32
    {
33
        $this->transformator = $transformator;
34
        empty($input) or $this->set($input);
35
    }
36
37
    /**
38
     * @param string|array $input
39
     * @return $this
40
     * @throws UnexpectedValueException
41
     */
42
    public function set($input)
43
    {
44
        if (is_string($input)) {
45
            $this->exchangeArray([$input]);
46
            return $this;
47
48
        } elseif (is_array($input)) {
49
            $this->exchangeArray($input);
50
            return $this;
51
52
        }
53
54
        throw new UnexpectedValueException(
55
            'Expected input as string or array, but provided ' . gettype($input)
56
        );
57
    }
58
59
    /**
60
     * @param NodeInterface $node
61
     * @param string $locator
62
     * @return \DOMNodeList
63
     * @throws InvalidArgumentException
64
     */
65
    public function locate(NodeInterface $node, $locator)
66
    {
67
        if (empty($node->ownerDocument) || !($node->ownerDocument instanceof Document)) {
0 ignored issues
show
Bug introduced by
Accessing ownerDocument on the interface WebinoDraw\Dom\NodeInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
68
            throw new InvalidArgumentException('Expects Dom\Document');
69
        }
70
        return $node->ownerDocument->getXpath()->query($this->set($locator)->xpathMatchAny(), $node);
0 ignored issues
show
Bug introduced by
Accessing ownerDocument on the interface WebinoDraw\Dom\NodeInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
71
    }
72
73
    /**
74
     * @param array $array
75
     * @return $this
76
     */
77
    public function merge(array $array)
78
    {
79
        $this->exchangeArray(array_merge($this->getArrayCopy(), $array));
80
        return $this;
81
    }
82
83
    /**
84
     * Xpath joined by OR
85
     *
86
     * @return string
87
     */
88
    public function xpathMatchAny()
89
    {
90
        $xpath = [];
91
        foreach ($this->getArrayCopy() as $locator) {
92
            $xpath[] = $this->transformator->locator2Xpath($this->normalizeLocator($locator));
93
        }
94
95
        return join('|', $xpath);
96
    }
97
98
    /**
99
     * @param string $locator
100
     * @return string
101
     */
102
    private function normalizeLocator($locator)
103
    {
104
        if (false === strpos($locator, PHP_EOL)) {
105
            return $locator;
106
        }
107
        return preg_replace('~[[:space:]]+~', ' ', $locator);
108
    }
109
}
110