Completed
Pull Request — master (#80)
by Tom
03:00
created

DefinitionParser::parse()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 41

Duplication

Lines 14
Ratio 34.15 %

Importance

Changes 0
Metric Value
dl 14
loc 41
rs 8.6417
c 0
b 0
f 0
cc 6
nc 12
nop 1
1
<?php
2
3
namespace Spatie\SchemaOrg\Generator\Parser;
4
5
use Symfony\Component\DomCrawler\Crawler;
6
use Spatie\SchemaOrg\Generator\Definitions;
7
use Spatie\SchemaOrg\Generator\TypeCollection;
8
use Spatie\SchemaOrg\Generator\Parser\Tasks\ParseType;
9
use Spatie\SchemaOrg\Generator\Parser\Tasks\ParseConstant;
10
use Spatie\SchemaOrg\Generator\Parser\Tasks\ParseProperty;
11
12
class DefinitionParser
13
{
14
    public function parse(Definitions $definitions): TypeCollection
15
    {
16
        $sources = $definitions->getSourceIds();
17
        $types = [];
18
        $properties = [];
19
        $constants = [];
20
21 View Code Duplication
        foreach ($sources as $sourceId) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
22
            $types = array_merge($types, array_filter($definitions
23
                ->query($sourceId, '[typeof="rdfs:Class"]')
24
                ->each(function (Crawler $crawler) {
25
                    return call_user_func(ParseType::fromCrawler($crawler));
26
                })));
27
        }
28
29 View Code Duplication
        foreach ($sources as $sourceId) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
30
            $properties = array_merge($properties, array_filter($definitions
31
                ->query($sourceId, '[typeof="rdf:Property"]')
32
                ->each(function (Crawler $crawler) {
33
                    return call_user_func(ParseProperty::fromCrawler($crawler));
34
                })));
35
        }
36
37
        foreach ($sources as $sourceId) {
38
            foreach ($types as $type) {
39
                $constants = array_merge($constants, array_filter($definitions
40
                    ->query($sourceId, '[typeof="'.$type->resource.'"]')
41
                    ->each(function (Crawler $crawler) use ($type) {
42
                        $constant = call_user_func(ParseConstant::fromCrawler($crawler));
43
44
                        if (! is_null($constant)) {
45
                            $constant->type = $type->name;
46
                        }
47
48
                        return $constant;
49
                    })));
50
            }
51
        }
52
53
        return new TypeCollection($types, $properties, $constants);
54
    }
55
}
56