Completed
Push — master ( 91a776...8c22b7 )
by Sebastian
07:46 queued 02:57
created

generator/Parser/Tasks/Task.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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