Completed
Push — master ( 23bc19...2eec4d )
by Tomáš
11:42
created

ClassLoaderDecorator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 44
ccs 0
cts 19
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A decorate() 0 18 3
A isDefaultStandard() 0 7 1
A detectStandardNamespaceFromStandardName() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of Symplify
5
 * Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz).
6
 */
7
8
namespace Symplify\PHP7_CodeSniffer\Composer;
9
10
use Composer\Autoload\ClassLoader;
11
use Symplify\PHP7_CodeSniffer\Standard\Finder\StandardFinder;
12
13
final class ClassLoaderDecorator
14
{
15
    /**
16
     * @var StandardFinder
17
     */
18
    private $standardFinder;
19
20
    public function __construct(StandardFinder $standardFinder)
21
    {
22
        $this->standardFinder = $standardFinder;
23
    }
24
25
    public function decorate(ClassLoader $classLoader)
26
    {
27
        $standards = $this->standardFinder->getStandards();
28
29
        foreach ($standards as $stadardName => $standardRuleset) {
30
            if ($this->isDefaultStandard($stadardName)) {
31
                continue;
32
            }
33
34
            $standardNamespace = $this->detectStandardNamespaceFromStandardName($stadardName);
35
            $standardDir = dirname($standardRuleset);
36
37
            $classLoader->addPsr4(
38
                $standardNamespace . '\\',
39
                $standardDir . DIRECTORY_SEPARATOR . $standardNamespace
40
            );
41
        }
42
    }
43
44
    private function isDefaultStandard(string $stadardName) : bool
45
    {
46
        return in_array(
47
            $stadardName,
48
            ['PSR1', 'MySource', 'PSR2', 'Zend', 'PEAR', 'Squiz', 'Generic']
49
        );
50
    }
51
52
    private function detectStandardNamespaceFromStandardName(string $standardName) : string
53
    {
54
        return str_replace(' ', '', $standardName);
55
    }
56
}
57