Failed Conditions
Pull Request — develop (#6873)
by
unknown
112:44 queued 47:41
created

Setup::createCacheInstance()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 39
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7.0222

Importance

Changes 0
Metric Value
cc 7
eloc 21
nc 7
nop 2
dl 0
loc 39
ccs 12
cts 13
cp 0.9231
crap 7.0222
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ORM\Tools;
6
7
use Doctrine\Common\Cache\ArrayCache;
8
use Doctrine\Common\Cache\Cache;
9
use Doctrine\Common\Cache\CacheProvider;
10
use Doctrine\Common\ClassLoader;
11
use Doctrine\ORM\Configuration;
12
use Doctrine\ORM\Mapping\Driver\XmlDriver;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\Mapping\Driver\XmlDriver was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
14
/**
15
 * Convenience class for setting up Doctrine from different installations and configurations.
16
 *
17
 * @author Benjamin Eberlei <[email protected]>
18
 */
19
class Setup
20
{
21
    /**
22
     * Use this method to register all autoloads for a downloaded Doctrine library.
23
     * Pick the directory the library was uncompressed into.
24
     *
25
     * @param string $directory
26
     *
27
     * @return void
28
     */
29
    public static function registerAutoloadDirectory($directory)
30
    {
31
        if (!class_exists('Doctrine\Common\ClassLoader', false)) {
32
            require_once $directory . "/Doctrine/Common/ClassLoader.php";
33
        }
34
35
        $loader = new ClassLoader("Doctrine", $directory);
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\ClassLoader has been deprecated: the ClassLoader is deprecated and will be removed in version 3.0 of doctrine/common. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

35
        $loader = /** @scrutinizer ignore-deprecated */ new ClassLoader("Doctrine", $directory);
Loading history...
36
        $loader->register();
37
38
        $loader = new ClassLoader('Symfony\Component', $directory . "/Doctrine");
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\ClassLoader has been deprecated: the ClassLoader is deprecated and will be removed in version 3.0 of doctrine/common. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

38
        $loader = /** @scrutinizer ignore-deprecated */ new ClassLoader('Symfony\Component', $directory . "/Doctrine");
Loading history...
39
        $loader->register();
40
    }
41
42
    /**
43
     * Creates a configuration with an annotation metadata driver.
44 1
     *
45
     * @param array   $paths
46 1
     * @param boolean $isDevMode
47 1
     * @param string  $proxyDir
48
     * @param Cache   $cache
49
     *
50 1
     * @return Configuration
51 1
     */
52
    public static function createAnnotationMetadataConfiguration(array $paths, $isDevMode = false, $proxyDir = null, Cache $cache = null)
53 1
    {
54 1
        $config = self::createConfiguration($isDevMode, $proxyDir, $cache);
55 1
        $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver($paths));
56
57
        return $config;
58
    }
59
60
    /**
61
     * Creates a configuration with a xml metadata driver.
62
     *
63
     * @param array   $paths
64
     * @param boolean $isDevMode
65
     * @param string  $proxyDir
66
     * @param Cache   $cache
67
     *
68 3
     * @return Configuration
69
     */
70 3
    public static function createXMLMetadataConfiguration(array $paths, $isDevMode = false, $proxyDir = null, Cache $cache = null)
71 3
    {
72
        $config = self::createConfiguration($isDevMode, $proxyDir, $cache);
73 3
        $config->setMetadataDriverImpl(new XmlDriver($paths));
74
75
        return $config;
76
    }
77
78
    /**
79
     * Creates a configuration without a metadata driver.
80
     *
81
     * @param bool   $isDevMode
82
     * @param string $proxyDir
83
     * @param Cache  $cache
84
     *
85
     * @return Configuration
86 1
     */
87
    public static function createConfiguration($isDevMode = false, $proxyDir = null, Cache $cache = null)
88 1
    {
89 1
        $proxyDir = $proxyDir ?: sys_get_temp_dir();
90
91 1
        $cache = self::createCacheConfiguration($isDevMode, $proxyDir, $cache);
92
93
        $config = new Configuration();
94
        $config->setMetadataCacheImpl($cache);
95
        $config->setQueryCacheImpl($cache);
96
        $config->setResultCacheImpl($cache);
97
        $config->setProxyDir($proxyDir);
98
        $config->setProxyNamespace('DoctrineProxies');
99
        $config->setAutoGenerateProxyClasses($isDevMode);
100
101
        return $config;
102
    }
103 5
104
    private static function createCacheConfiguration(bool $isDevMode, string $proxyDir, ?Cache $cache) :  Cache
105 5
    {
106
        $cache = self::createCacheInstance($isDevMode, $cache);
107 5
108
        if ( ! $cache instanceof CacheProvider) {
109
            return $cache;
110
        }
111
112
        $namespace = $cache->getNamespace();
113
114
        if ($namespace !== '') {
115
            $namespace .= ':';
116
        }
117
118
        $cache->setNamespace($namespace . 'dc2_' . md5($proxyDir) . '_'); // to avoid collisions
119
120
        return $cache;
121
    }
122
123
    private static function createCacheInstance(bool $isDevMode, ?Cache $cache) : Cache
124
    {
125 5
        if ($cache !== null) {
126 3
            return $cache;
127
        }
128
129 5
        if ($isDevMode === true) {
130 4
            return new ArrayCache();
131
        }
132
133 5
        if (extension_loaded('apc')) {
134 5
            return new \Doctrine\Common\Cache\ApcCache();
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\Cache\ApcCache has been deprecated: since version 1.6, use ApcuCache instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

134
            return /** @scrutinizer ignore-deprecated */ new \Doctrine\Common\Cache\ApcCache();
Loading history...
135 5
        }
136 5
137 5
        if (ini_get('xcache.cacher')) {
138 5
            return new \Doctrine\Common\Cache\XcacheCache();
139 5
        }
140
141 5
        if (extension_loaded('memcache')) {
142
            $memcache = new \Memcache();
143
            $memcache->connect('127.0.0.1');
0 ignored issues
show
Bug introduced by
The call to MemcachePool::connect() has too few arguments starting with port. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

143
            $memcache->/** @scrutinizer ignore-call */ 
144
                       connect('127.0.0.1');

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
144
145
            $cache = new \Doctrine\Common\Cache\MemcacheCache();
146
            $cache->setMemcache($memcache);
147
148
            return $cache;
149
        }
150
151
        if (extension_loaded('redis')) {
152
            $redis = new \Redis();
153
            $redis->connect('127.0.0.1');
154
155
            $cache = new \Doctrine\Common\Cache\RedisCache();
156
            $cache->setRedis($redis);
157
158
            return $cache;
159
        }
160
161
        return new ArrayCache();
162
    }
163
}
164