Passed
Push — master ( 8164eb...1b7218 )
by Guillaume
13:39
created

RickAstley::removeHeader()   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 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace App\Docsets;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Support\Collection;
7
use Wa72\HtmlPageDom\HtmlPageCrawler;
8
use Illuminate\Support\Facades\Storage;
9
10
class RickAstley extends BaseDocset
11
{
12
    public const CODE = 'rick-astley';
13
    public const NAME = 'Rick Astley';
14
    public const URL = 'rickastley.co.uk';
15
    public const INDEX = 'index.html';
16
    public const PLAYGROUND = '';
17
    public const ICON_16 = 'icons/favicon-16x16.png';
18
    public const ICON_32 = 'icons/favicon-32x32.png';
19
    public const EXTERNAL_DOMAINS = [
20
        'fonts.googleapis.com',
21
        'widget.songkick.com',
22
        'cdn-images.mailchimp.com',
23
        's3.amazonaws.com',
24
        'ajax.googleapis.com'
25
    ];
26
27
28 12
    public function entries(string $file): Collection
29
    {
30 12
        $entries = collect();
31
32 12
        $crawler = HtmlPageCrawler::create(Storage::get($file));
33
34 12
        if (Str::contains($file, "{$this->url()}/index.html")) {
35
            $crawler->filter('#main_menu li:not(:first-child) a')->each(function (HtmlPageCrawler $node) use ($entries) {
0 ignored issues
show
Unused Code introduced by
The parameter $node is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

35
            $crawler->filter('#main_menu li:not(:first-child) a')->each(function (/** @scrutinizer ignore-unused */ HtmlPageCrawler $node) use ($entries) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
36 12
                $entries->push([
37 12
                    'name' => 'Rick Astley - Official Website',
38 12
                    'type' => 'Guide',
39 12
                    'path' => $this->url() . '/index.html'
40
                ]);
41 12
            });
42
        }
43
44
        $crawler->filter('#main_menu li:not(:first-child) a')->each(function (HtmlPageCrawler $node) use ($entries) {
45 12
            $entries->push([
46 12
                'name' => $node->text(),
47 12
                'type' => 'Section',
48 12
                'path' => $this->url() . '/' . $node->attr('href')
49
            ]);
50 12
        });
51
52 12
        return $entries;
53
    }
54
55 12
    public function format(string $html): string
56
    {
57 12
        $crawler = HtmlPageCrawler::create($html);
58
59 12
        $this->removeHeader($crawler);
60 12
        $this->removeFooter($crawler);
61
62 12
        $this->removeUnwantedHTML($crawler);
63 12
        $this->removeUnwantedJavaScript($crawler);
64
65 12
        $this->insertDashTableOfContents($crawler);
66
67 12
        return $crawler->saveHTML();
68
    }
69
70 12
    protected function removeHeader(HtmlPageCrawler $crawler)
71
    {
72 12
        $crawler->filter('#header')->remove();
73 12
    }
74
75 12
    protected function removeFooter(HtmlPageCrawler $crawler)
76
    {
77 12
        $crawler->filter('#footer')->remove();
78 12
    }
79
80 12
    protected function removeUnwantedHTML(HtmlPageCrawler $crawler)
81
    {
82 12
        $crawler->filterXPath("//img[@src[contains(.,'secure.adnxs.com')]]")->remove();
83 12
    }
84
85 12
    protected function removeUnwantedJavaScript(HtmlPageCrawler $crawler)
86
    {
87 12
        $crawler->filter('noscript')->remove();
88 12
        $crawler->filterXPath("//script[@src[contains(.,'platform.twitter.com')]]")->remove();
89 12
        $crawler->filterXPath("//script[@src[contains(.,'googletagmanager')]]")->remove();
90 12
        $crawler->filterXPath("//script[@src[contains(.,'googleadservices')]]")->remove();
91 12
        $crawler->filterXPath("//script[text()[contains(.,'googletagmanager')]]")->remove();
92 12
        $crawler->filterXPath("//script[text()[contains(.,'gtag')]]")->remove();
93 12
        $crawler->filterXPath("//script[text()[contains(.,'connect.facebook.net')]]")->remove();
94 12
        $crawler->filterXPath("//script[text()[contains(.,'google_conversion_id')]]")->remove();
95 12
        $crawler->filterXPath("//script[text()[contains(.,'googleadservices')]]")->remove();
96 12
        $crawler->filterXPath("//script[text()[contains(.,'platform.twitter.com')]]")->remove();
97 12
        $crawler->filterXPath("//script[text()[contains(.,'twttr.conversion')]]")->remove();
98 12
    }
99
100 12
    protected function insertDashTableOfContents(HtmlPageCrawler $crawler)
101
    {
102 12
        $crawler->filter('head')
103 12
            ->before('<a name="//apple_ref/cpp/Section/Top" class="dashAnchor"></a>');
104
105
        $crawler->filter('div.page_title, div.product_title')->each(static function (HtmlPageCrawler $node) {
106 12
            $node->before(
107 12
                '<a id="' . Str::slug($node->text()) . '" name="//apple_ref/cpp/Section/' . rawurlencode($node->text()) . '" class="dashAnchor"></a>'
108
            );
109 12
        });
110 12
    }
111
}
112