Completed
Push — master ( 6f9845...68f10d )
by Marco
12:19 queued 14s
created

Psr4Mapping   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 96
Duplicated Lines 36.46 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 35
loc 96
c 0
b 0
f 0
wmc 13
lcom 1
cbo 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A matchingPrefixes() 0 10 1
A directories() 0 4 1
A __construct() 0 3 1
A fromArrayMappings() 17 17 1
A resolvePossibleFilePaths() 0 24 3
A assertValidMapping() 18 18 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Roave\BetterReflection\SourceLocator\Type\Composer\Psr;
6
7
use Roave\BetterReflection\Identifier\Identifier;
8
use Roave\BetterReflection\SourceLocator\Type\Composer\Psr\Exception\InvalidPrefixMapping;
9
use const ARRAY_FILTER_USE_KEY;
10
use function array_filter;
11
use function array_keys;
12
use function array_map;
13
use function array_merge;
14
use function array_unique;
15
use function array_values;
16
use function is_dir;
17
use function ltrim;
18
use function rtrim;
19
use function str_replace;
20
use function strlen;
21
use function strpos;
22
use function substr;
23
24
final class Psr4Mapping implements PsrAutoloaderMapping
25
{
26
    /** @var array<string, array<int, string>> */
27
    private $mappings = [];
28
29
    private function __construct()
30
    {
31
    }
32
33
    /** @param array<string, array<int, string>> $mappings */
34 View Code Duplication
    public static function fromArrayMappings(array $mappings) : self
0 ignored issues
show
Duplication introduced by Marco Pivetta
This method seems to be duplicated in 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...
35
    {
36
        self::assertValidMapping($mappings);
37
38
        $instance = new self();
39
40
        $instance->mappings = array_map(
41
            static function (array $directories) : array {
42
                return array_map(static function (string $directory) : string {
43
                    return rtrim($directory, '/');
44
                }, $directories);
45
            },
46
            $mappings
47
        );
48
49
        return $instance;
50
    }
51
52
    /** {@inheritDoc} */
53
    public function resolvePossibleFilePaths(Identifier $identifier) : array
54
    {
55
        if (! $identifier->isClass()) {
56
            return [];
57
        }
58
59
        $className        = $identifier->getName();
60
        $matchingPrefixes = $this->matchingPrefixes($className);
61
62
        return array_values(array_filter(array_merge(
63
            [],
64
            ...array_map(static function (array $paths, string $prefix) use ($className) : array {
65
                $subPath = ltrim(str_replace('\\', '/', substr($className, strlen($prefix))), '/');
66
67
                if ($subPath === '') {
68
                    return [];
69
                }
70
71
                return array_map(static function (string $path) use ($subPath) : string {
72
                    return rtrim($path, '/') . '/' . $subPath . '.php';
73
                }, $paths);
74
            }, $matchingPrefixes, array_keys($matchingPrefixes))
75
        )));
76
    }
77
78
    /** @return array<string, array<int, string>> */
0 ignored issues
show
Documentation introduced by Marco Pivetta
The doc-type array<string, could not be parsed: Expected ">" at position 5, but found "end of type". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
79
    private function matchingPrefixes(string $className) : array
80
    {
81
        return array_filter(
82
            $this->mappings,
83
            static function (string $prefix) use ($className) : bool {
84
                return strpos($className, $prefix) === 0;
85
            },
86
            ARRAY_FILTER_USE_KEY
87
        );
88
    }
89
90
    /** {@inheritDoc} */
91
    public function directories() : array
92
    {
93
        return array_values(array_unique(array_merge([], ...array_values($this->mappings))));
94
    }
95
96
    /**
97
     * @param array<string, array<int, string>> $mappings
98
     *
99
     * @throws InvalidPrefixMapping
100
     */
101 View Code Duplication
    private static function assertValidMapping(array $mappings) : void
0 ignored issues
show
Duplication introduced by Marco Pivetta
This method seems to be duplicated in 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...
102
    {
103
        foreach ($mappings as $prefix => $paths) {
104
            if ($prefix === '') {
105
                throw InvalidPrefixMapping::emptyPrefixGiven();
106
            }
107
108
            if ($paths === []) {
109
                throw InvalidPrefixMapping::emptyPrefixMappingGiven($prefix);
110
            }
111
112
            foreach ($paths as $path) {
113
                if (! is_dir($path)) {
114
                    throw InvalidPrefixMapping::prefixMappingIsNotADirectory($prefix, $path);
115
                }
116
            }
117
        }
118
    }
119
}
120