HtmlPlugin::getTriggers()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2013 Jonathan Vollebregt ([email protected]), Rokas Šleinius ([email protected])
9
 *
10
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
11
 * this software and associated documentation files (the "Software"), to deal in
12
 * the Software without restriction, including without limitation the rights to
13
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
14
 * the Software, and to permit persons to whom the Software is furnished to do so,
15
 * subject to the following conditions:
16
 *
17
 * The above copyright notice and this permission notice shall be included in all
18
 * copies or substantial portions of the Software.
19
 *
20
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
22
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
23
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
24
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
 */
27
28
namespace Kint\Parser;
29
30
use Dom\HTMLDocument;
0 ignored issues
show
Bug introduced by
The type Dom\HTMLDocument 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...
31
use DOMException;
32
use Kint\Value\AbstractValue;
33
use Kint\Value\Context\BaseContext;
34
use Kint\Value\Representation\ContainerRepresentation;
35
use Kint\Value\Representation\ValueRepresentation;
36
37
class HtmlPlugin extends AbstractPlugin implements PluginCompleteInterface
38
{
39
    public function getTypes(): array
40
    {
41
        return ['string'];
42
    }
43
44
    public function getTriggers(): int
45
    {
46
        if (!KINT_PHP84) {
47
            return Parser::TRIGGER_NONE; // @codeCoverageIgnore
48
        }
49
50
        return Parser::TRIGGER_SUCCESS;
51
    }
52
53
    public function parseComplete(&$var, AbstractValue $v, int $trigger): AbstractValue
54
    {
55
        if ('<!doctype html>' !== \strtolower(\substr($var, 0, 15))) {
56
            return $v;
57
        }
58
59
        try {
60
            $html = HTMLDocument::createFromString($var, LIBXML_NOERROR);
61
        } catch (DOMException $e) { // @codeCoverageIgnore
62
            return $v; // @codeCoverageIgnore
63
        }
64
65
        $c = $v->getContext();
66
67
        $base = new BaseContext('childNodes');
68
        $base->depth = $c->getDepth();
69
70
        if (null !== ($ap = $c->getAccessPath())) {
71
            $base->access_path = '\\Dom\\HTMLDocument::createFromString('.$ap.')->childNodes';
72
        }
73
74
        $out = $this->getParser()->parse($html->childNodes, $base);
75
        $iter = $out->getRepresentation('iterator');
76
77
        if ($out->flags & AbstractValue::FLAG_DEPTH_LIMIT) {
78
            $out->flags |= AbstractValue::FLAG_GENERATED;
79
            $v->addRepresentation(new ValueRepresentation('HTML', $out), 0);
80
        } elseif ($iter instanceof ContainerRepresentation) {
81
            $v->addRepresentation(new ContainerRepresentation('HTML', $iter->getContents()), 0);
82
        }
83
84
        return $v;
85
    }
86
}
87