Conditions | 18 |
Paths | 1 |
Total Lines | 144 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
27 | public function register(ContainerInterface $container) |
||
28 | { |
||
29 | $container['cache.default_options'] = [ |
||
30 | 'driver' => 'array', |
||
31 | 'namespace' => null, |
||
32 | ]; |
||
33 | |||
34 | $container['caches.options.initializer'] = $container->protect(function () use ($container) { |
||
35 | static $initialized = false; |
||
36 | |||
37 | if ($initialized) { |
||
38 | return; |
||
39 | } |
||
40 | |||
41 | $initialized = true; |
||
42 | |||
43 | if (!isset($container['caches.options'])) { |
||
44 | $container['caches.options'] = [ |
||
45 | 'default' => $container['cache.options'] ?? [], |
||
46 | ]; |
||
47 | } |
||
48 | |||
49 | $container['caches.options'] = array_map(function ($options) use ($container) { |
||
50 | return array_replace($container['cache.default_options'], is_array($options) |
||
51 | ? $options |
||
52 | : ['driver' => $options] |
||
53 | ); |
||
54 | }, $container['caches.options']); |
||
55 | |||
56 | if (!isset($container['caches.default'])) { |
||
57 | $container['caches.default'] = array_keys( |
||
58 | array_slice($container['caches.options'], 0, 1) |
||
59 | )[0]; |
||
60 | } |
||
61 | }); |
||
62 | |||
63 | $container['caches'] = function (ContainerInterface $container) { |
||
64 | $container['caches.options.initializer'](); |
||
65 | |||
66 | $caches = new Container(); |
||
67 | foreach ($container['caches.options'] as $name => $options) { |
||
68 | $caches[$name] = function () use ($container, $options) { |
||
69 | $cache = $container['cache_factory']($options['driver'], $options); |
||
70 | |||
71 | if (isset($options['namespace'])) { |
||
72 | $cache->setNamespace($options['namespace']); |
||
73 | } |
||
74 | |||
75 | return $cache; |
||
76 | }; |
||
77 | } |
||
78 | |||
79 | return $caches; |
||
80 | }; |
||
81 | |||
82 | $container['cache_factory.filesystem'] = $container->protect(function ($options) { |
||
83 | if (empty($options['cache_dir']) |
||
84 | || false === is_dir($options['cache_dir']) |
||
85 | ) { |
||
86 | throw new \InvalidArgumentException( |
||
87 | 'You must specify "cache_dir" for Filesystem.' |
||
88 | ); |
||
89 | } |
||
90 | |||
91 | return new FilesystemCache($options['cache_dir']); |
||
92 | }); |
||
93 | |||
94 | $container['cache_factory.array'] = $container->protect(function ($options) { |
||
|
|||
95 | return new ArrayCache(); |
||
96 | }); |
||
97 | |||
98 | $container['cache_factory.apcu'] = $container->protect(function ($options) { |
||
99 | return new ApcuCache(); |
||
100 | }); |
||
101 | |||
102 | $container['cache_factory.mongodb'] = $container->protect(function ($options) { |
||
103 | if (empty($options['server']) |
||
104 | || empty($options['name']) |
||
105 | || empty($options['collection']) |
||
106 | ) { |
||
107 | throw new \InvalidArgumentException( |
||
108 | 'You must specify "server", "name" and "collection" for MongoDB.' |
||
109 | ); |
||
110 | } |
||
111 | |||
112 | $client = new \MongoClient($options['server']); |
||
113 | $db = new \MongoDB($client, $options['name']); |
||
114 | $collection = new \MongoCollection($db, $options['collection']); |
||
115 | |||
116 | return new MongoDBCache($collection); |
||
117 | }); |
||
118 | |||
119 | $container['cache_factory.redis'] = $container->protect(function ($options) { |
||
120 | if (empty($options['host']) || empty($options['port'])) { |
||
121 | throw new \InvalidArgumentException('You must specify "host" and "port" for Redis.'); |
||
122 | } |
||
123 | |||
124 | $redis = new \Redis(); |
||
125 | $redis->connect($options['host'], $options['port']); |
||
126 | |||
127 | if (isset($options['password'])) { |
||
128 | $redis->auth($options['password']); |
||
129 | } |
||
130 | |||
131 | $cache = new RedisCache(); |
||
132 | $cache->setRedis($redis); |
||
133 | |||
134 | return $cache; |
||
135 | }); |
||
136 | |||
137 | $container['cache_factory.xcache'] = $container->protect(function ($options) { |
||
138 | |||
139 | if (empty($options['host']) || empty($options['port'])) { |
||
140 | throw new \InvalidArgumentException('You must specify "host" and "port" for Memcached.'); |
||
141 | } |
||
142 | |||
143 | $memcached = new \Memcached(); |
||
144 | $memcached->addServer($options['host'], $options['port']); |
||
145 | |||
146 | $cache = new MemcachedCache(); |
||
147 | $cache->setMemcached($memcached); |
||
148 | |||
149 | return $cache; |
||
150 | }); |
||
151 | |||
152 | $container['cache_factory.xcache'] = $container->protect(function ($options) { |
||
153 | return new XcacheCache(); |
||
154 | }); |
||
155 | |||
156 | $container['cache_factory'] = $container->protect(function ($driver, $options) use ($container) { |
||
157 | if (isset($container['cache_factory.' . $driver])) { |
||
158 | return $container['cache_factory.' . $driver]($options); |
||
159 | } |
||
160 | |||
161 | throw new \RuntimeException(); |
||
162 | }); |
||
163 | |||
164 | // shortcuts for the "first" cache |
||
165 | $container['cache'] = function (Container $container) { |
||
166 | $caches = $container['caches']; |
||
167 | |||
168 | return $caches[$container['caches.default']]; |
||
169 | }; |
||
170 | } |
||
171 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.