Completed
Pull Request — feat/html-splitter (#184)
by Nuno
13:52 queued 09:30
created

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