Passed
Pull Request — master (#170)
by
unknown
08:08
created

NodeCollection::findWeight()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Scout Extended.
7
 *
8
 * (c) Algolia Team <[email protected]>
9
 *
10
 *  For the full copyright and license information, please view the LICENSE
11
 *  file that was distributed with this source code.
12
 */
13
14
namespace Algolia\ScoutExtended\Splitters\HtmlSplitter;
15
16
/**
17
 * @internal
18
 */
19
final class NodeCollection
20
{
21
    /**
22
     * Collection of \Algolia\ScoutExtended\Splitters\HtmlSplitter\Node.
23
     *
24
     * @var array
25
     */
26
    private $nodes = [];
27
28
    /**
29
     * @var \Algolia\ScoutExtended\Splitters\HtmlSplitter\NodesCollection.
0 ignored issues
show
Bug introduced by
The type Algolia\ScoutExtended\Sp...litter\NodesCollection. was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
     */
31
    private $nodesCollection;
32
    /**
33
     * The list of html tags.
34
     *
35
     * @var string[]
36
     */
37
    private $tags = [];
38
39
    /**
40
     * String.
41
     */
42
    private const PARAGRAPH = 'p';
43
44
    /**
45
     * NodeCollection constructor.
46
     *
47
     * @param array|null $tags
48
     * @param \Algolia\ScoutExtended\Splitters\HtmlSplitter\NodesCollection $nodesCollection
49
     */
50 8
    public function __construct(array $tags = null, NodesCollection $nodesCollection)
51
    {
52 8
        if ($tags !== null) {
53 8
            $this->tags = $tags;
54
        }
55 8
        $this->nodesCollection = $nodesCollection;
0 ignored issues
show
Documentation Bug introduced by
It seems like $nodesCollection of type Algolia\ScoutExtended\Sp...plitter\NodesCollection is incompatible with the declared type Algolia\ScoutExtended\Sp...litter\NodesCollection. of property $nodesCollection.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
56 8
    }
57
58
    /**
59
     * @return array
60
     */
61 8
    public function getNodes(): array
62
    {
63 8
        return $this->nodes;
64
    }
65
66
    /**
67
     * Add object to collection.
68
     *
69
     * @param \Algolia\ScoutExtended\Splitters\HtmlSplitter\Node $node
70
     */
71 8
    public function push(Node $node): void
72
    {
73 8
        if ($this->lengthNodes() === 0 || $this->findWeight($node) > $this->findWeight($this->last(0))) {
74 8
            $this->nodes[] = $node;
75 8
            $this->nodesCollection->push($this);
76
        } else {
77 4
            array_pop($this->nodes);
78 4
            $this->push($node);
79
        }
80 8
    }
81
82
    /**
83
     * Return the last element of the collection
84
     * Give integer as pointer from the end.
85
     *
86
     * @param int $position
87
     *
88
     * @return \Algolia\ScoutExtended\Splitters\HtmlSplitter\Node
89
     */
90 8
    public function last(int $position): Node
91
    {
92 8
        return $this->nodes[$this->lengthNodes() - $position - 1];
93
    }
94
95
    /**
96
     * Importance formula.
97
     * Give integer from tags ranking.
98
     *
99
     * @param \Algolia\ScoutExtended\Splitters\HtmlSplitter\Node $node
100
     *
101
     * @return int
102
     */
103 8
    public function importanceWeight(Node $node): int
104
    {
105 8
        if ($node->getTag() === self::PARAGRAPH) {
106 4
            if ($this->last(0) === null || $this->lengthNodes() === 1) {
107 1
                return 0;
108
            }
109 3
            $object = $this->last(1);
110
111 3
            return (count($this->tags) - 1) + $this->findWeight($object);
112
        }
113
114 7
        return $this->findWeight($node);
115
    }
116
117
    /**
118
     * Find weight of current nodes.
119
     *
120
     * @param \Algolia\ScoutExtended\Splitters\HtmlSplitter\Node $node
121
     *
122
     * @return int
123
     */
124 7
    private function findWeight(Node $node): int
125
    {
126 7
        return (int) array_search($node->getTag(), $this->tags, true);
127
    }
128
129
    /**
130
     * Give the length of the collection.
131
     *
132
     * @return int
133
     */
134 8
    private function lengthNodes(): int
135
    {
136 8
        return count($this->nodes);
137
    }
138
}
139