GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

YamlCachedContainerBuilder   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 15
c 4
b 0
f 1
lcom 1
cbo 5
dl 0
loc 117
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B build() 0 29 3
B checkNamespace() 0 24 6
A checkClassName() 0 6 2
A isNameValid() 0 5 3
1
<?php
2
3
namespace Symfony\Component\DependencyInjection;
4
5
use Symfony\Component\Config\ConfigCache;
6
use Symfony\Component\Config\FileLocator;
7
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
8
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
9
10
class YamlCachedContainerBuilder
11
{
12
    /**
13
     * @var string
14
     */
15
    private $cacheDir;
16
17
    /**
18
     * @var bool
19
     */
20
    private $debug;
21
22
    /**
23
     * Constructor.
24
     *
25
     * @param string $cacheDir
26
     * @param bool   $debug
27
     */
28
    public function __construct($cacheDir, $debug = false)
29
    {
30
        $this->cacheDir = $cacheDir;
31
        $this->debug = $debug;
32
    }
33
34
    /**
35
     * Build the container.
36
     *
37
     * @param string $configDir
38
     * @param string $configFile
39
     * @param string $className
40
     * @param string $namespace
41
     *
42
     * @return Container
43
     */
44
    public function build($configDir, $configFile, $className = 'CachedContainer', $namespace = 'Cache')
45
    {
46
        if ($this->debug) { // provides useful info regarding namespace when in debug, ignored in prod for performance
47
            self::checkNamespace($namespace);
48
            self::checkClassName($className);
49
        }
50
51
        $fqClassName = '\\' . $namespace . '\\' . $className;
52
        $cacheFile = $this->cacheDir . '/' . $className . '.php';
53
        $configCache = new ConfigCache($cacheFile, $this->debug);
54
55
        if (!$configCache->isFresh()) {
56
            $containerBuilder = new ContainerBuilder();
57
            $loader = new YamlFileLoader(
58
                $containerBuilder,
59
                new FileLocator($configDir)
60
            );
61
            $loader->load($configFile);
62
            $containerBuilder->compile();
63
64
            $dumper = new PhpDumper($containerBuilder);
65
            $configCache->write(
66
                $dumper->dump(['class' => $className, 'namespace' => $namespace]),
67
                $containerBuilder->getResources()
68
            );
69
        }
70
71
        return new $fqClassName();
72
    }
73
74
    /**
75
     * Format the namespace to avoid any problem.
76
     *
77
     * @param $namespace
78
     *
79
     * @return string
80
     */
81
    private static function checkNamespace($namespace)
82
    {
83
        if (empty($namespace)) {
84
            throw new \InvalidArgumentException('Namespace cannot be empty');
85
        }
86
87
        if (strrpos($namespace, '\\') === 0) { // if namespace begins with \
88
            throw new \InvalidArgumentException(sprintf('Namespace "%s" cannot begin with \\', $namespace));
89
        }
90
91
        if (strrpos($namespace, '\\') === strlen($namespace) - 1) { // if namespace is ending with \
92
            throw new \InvalidArgumentException(sprintf('Namespace "%s" cannot end with \\', $namespace));
93
        }
94
95
        // check if each part of the namespace is valid
96
        $parts = explode('\\', $namespace);
97
        foreach ($parts as $part) {
98
            if (!self::isNameValid($part)) {
99
                throw new \InvalidArgumentException(
100
                    sprintf('Invalid namespace part : "%s" for "%s"', $part, $namespace)
101
                );
102
            }
103
        }
104
    }
105
106
    /**
107
     * @param $className
108
     */
109
    private static function checkClassName($className)
110
    {
111
        if (!self::isNameValid($className)) {
112
            throw new \InvalidArgumentException(sprintf('Class name "%" is not valid', $className));
113
        }
114
    }
115
116
    /**
117
     * @param $name
118
     *
119
     * @return bool
120
     */
121
    private static function isNameValid($name)
122
    {
123
        return preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $name, $matches) === 1 &&
124
            count($matches) === 1 && $matches[0] === $name;
125
    }
126
}
127