Passed
Push — master ( 44266f...d1b9e8 )
by ReliQ
04:53
created

SimplePatternExtractor   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 8
dl 0
loc 34
rs 10
c 1
b 0
f 1
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A extract() 0 13 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ReliqArts\StyleImporter\CSS\Extractor;
6
7
use ReliqArts\StyleImporter\CSS\Extractable;
8
use ReliqArts\StyleImporter\CSS\Extractor;
9
use ReliqArts\StyleImporter\CSS\Rule\Import;
10
11
abstract class SimplePatternExtractor implements Extractor
12
{
13
    /**
14
     * @param string $styles
15
     *
16
     * @return Import[]
17
     */
18
    public function extract(string $styles): array
19
    {
20
        $matchCount = preg_match_all($this->getPattern(), $styles, $matches);
21
22
        if (empty($matchCount)) {
23
            return [];
24
        }
25
26
        return array_map(
27
            function (string $match): Extractable {
28
                return $this->createExtractable($match);
29
            },
30
            $matches[1]
31
        );
32
    }
33
34
    /**
35
     * @return string
36
     */
37
    abstract protected function getPattern(): string;
38
39
    /**
40
     * @param string $match
41
     *
42
     * @return Extractable
43
     */
44
    abstract protected function createExtractable(string $match): Extractable;
45
}
46