Total Complexity | 11 |
Total Lines | 58 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
5 | trait HarvestLinksTrait |
||
6 | { |
||
7 | /** |
||
8 | * @var array |
||
9 | */ |
||
10 | protected $links; |
||
11 | |||
12 | protected $linksPerType; |
||
13 | |||
14 | abstract public function getDom(); |
||
15 | 3 | ||
16 | public function getLinkedRessources() |
||
17 | 3 | { |
|
18 | return ExtractLinks::get($this, ExtractLinks::SELECT_ALL); |
||
19 | } |
||
20 | 9 | ||
21 | public function getLinks($type = null): array |
||
22 | 9 | { |
|
23 | 6 | if (null === $this->links) { |
|
24 | 6 | $this->links = ExtractLinks::get($this, ExtractLinks::SELECT_A); |
|
25 | $this->classifyLinks(); |
||
26 | } |
||
27 | |||
28 | 9 | switch ($type) { |
|
29 | 6 | case Link::LINK_SELF: |
|
30 | 9 | return $this->linksPerType[Link::LINK_SELF] ?? []; |
|
31 | 6 | case Link::LINK_INTERNAL: |
|
32 | 9 | return $this->linksPerType[Link::LINK_INTERNAL] ?? []; |
|
33 | 6 | case Link::LINK_SUB: |
|
34 | 9 | return $this->linksPerType[Link::LINK_SUB] ?? []; |
|
35 | 6 | case Link::LINK_EXTERNAL: |
|
36 | return $this->linksPerType[Link::LINK_EXTERNAL] ?? []; |
||
37 | 9 | default: |
|
38 | return $this->links; |
||
39 | } |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Return duplicate links |
||
44 | * /test and /test#2 are not duplicates. |
||
45 | 3 | */ |
|
46 | public function getNbrDuplicateLinks(): int |
||
47 | 3 | { |
|
48 | 3 | $links = $this->getLinks(); |
|
49 | 3 | $u = []; |
|
50 | 3 | foreach ($links as $link) { |
|
51 | $u[(string) $link->getUrl()] = 1; |
||
52 | } |
||
53 | 3 | ||
54 | return \count($links) - \count($u); |
||
55 | } |
||
56 | 6 | ||
57 | public function classifyLinks() |
||
63 | 6 | } |
|
64 | } |
||
65 | } |
||
66 |