Completed
Push — master ( 4d90dd...0c32ab )
by Sebastian
03:54
created

Task::getText()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 4
nop 2
1
<?php
2
3
namespace Spatie\SchemaOrg\Generator\Parser\Tasks;
4
5
use Symfony\Component\DomCrawler\Crawler;
6
7
abstract class Task
8
{
9
    /** @string */
10
    protected $definition;
11
12
    public function __construct(string $definition)
13
    {
14
        $this->definition = $definition;
15
    }
16
17
    public static function fromCrawler(Crawler $crawler): self
18
    {
19
        $node = $crawler->getNode(0);
20
        $html = $node->ownerDocument->saveHTML($node);
21
22
        return new static($html);
23
    }
24
25
    protected function getText(Crawler $node, string $selector = null): string
26
    {
27
        if ($selector) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $selector of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
28
            $node = $node->filter($selector)->first();
29
        }
30
31
        if ($node->count() === 0) {
32
            return '';
33
        }
34
35
        return trim($node->text());
36
    }
37
38
    protected function getAttribute(Crawler $node, string $attribute): string
39
    {
40
        if ($node->count() === 0) {
41
            return '';
42
        }
43
44
        return $node->filter("[{$attribute}]")->attr($attribute) ?? '';
45
    }
46
}
47