Skip to content
This repository was archived by the owner on Feb 6, 2020. It is now read-only.

Commit 1db4334

Browse files
committed
Ensure config file uses ::class notation everywhere
Added `createConfigKey()`, which will ensure the returned key is a string, a `::class` reference, or a null value (indicating no key is necessary; previously, integer keys were emitted). Also added `createConfigValue()`, to ensure that string values that resolve to classes are emitted as `::class` notation. Finally, added docblocks throughout.
1 parent 75cda56 commit 1db4334

File tree

1 file changed

+79
-26
lines changed

1 file changed

+79
-26
lines changed

src/Tool/CliTool.php

Lines changed: 79 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
namespace Zend\ServiceManager\Tool;
1111

12+
use ReflectionClass;
13+
use ReflectionParameter;
1214
use Traversable;
1315
use Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory;
1416
use Zend\ServiceManager\Exception\InvalidArgumentException;
@@ -27,23 +29,23 @@ class CliTool
2729

2830
/**
2931
* @param array $config
30-
* @param $className
32+
* @param string $className
3133
* @return array
32-
* @throws InvalidArgumentException
34+
* @throws InvalidArgumentException for invalid $className
3335
*/
3436
public static function createDependencyConfig(array $config, $className)
3537
{
3638
self::validateClassName($className);
3739

38-
$reflectionClass = new \ReflectionClass($className);
40+
$reflectionClass = new ReflectionClass($className);
3941
if (! $reflectionClass->getConstructor()) {
4042
return $config;
4143
}
4244

4345
$constructorArguments = $reflectionClass->getConstructor()->getParameters();
4446
$constructorArguments = array_filter(
4547
$constructorArguments,
46-
function (\ReflectionParameter $argument) {
48+
function (ReflectionParameter $argument) {
4749
return ! $argument->isOptional();
4850
}
4951
);
@@ -70,20 +72,11 @@ function (\ReflectionParameter $argument) {
7072
}
7173

7274
/**
73-
* @param $className
74-
* @throws InvalidArgumentException
75+
* @param array $config
76+
* @return array
77+
* @throws InvalidArgumentException if ConfigAbstractFactory configuration
78+
* value is not an array.
7579
*/
76-
private static function validateClassName($className)
77-
{
78-
if (! is_string($className)) {
79-
throw new InvalidArgumentException('Class name must be a string, ' . gettype($className) . ' given');
80-
}
81-
82-
if (! class_exists($className)) {
83-
throw new InvalidArgumentException('Cannot find class with name ' . $className);
84-
}
85-
}
86-
8780
public static function createFactoryMappingsFromConfig(array $config)
8881
{
8982
if (! array_key_exists(ConfigAbstractFactory::class, $config)) {
@@ -104,6 +97,11 @@ public static function createFactoryMappingsFromConfig(array $config)
10497
return $config;
10598
}
10699

100+
/**
101+
* @param array $config
102+
* @param string $className
103+
* @return array
104+
*/
107105
public static function createFactoryMappings(array $config, $className)
108106
{
109107
self::validateClassName($className);
@@ -119,28 +117,48 @@ public static function createFactoryMappings(array $config, $className)
119117
return $config;
120118
}
121119

120+
/**
121+
* @param array $config
122+
* @return string
123+
*/
122124
public static function dumpConfigFile(array $config)
123125
{
124126
$prepared = self::prepareConfig($config);
125127
return sprintf(self::CONFIG_TEMPLATE, date('Y-m-d H:i:s'), $prepared);
126128
}
127129

130+
/**
131+
* @param $className
132+
* @throws InvalidArgumentException if class name is not a string or does
133+
* not exist.
134+
*/
135+
private static function validateClassName($className)
136+
{
137+
if (! is_string($className)) {
138+
throw new InvalidArgumentException('Class name must be a string, ' . gettype($className) . ' given');
139+
}
140+
141+
if (! class_exists($className)) {
142+
throw new InvalidArgumentException('Cannot find class with name ' . $className);
143+
}
144+
}
145+
146+
/**
147+
* @param array|Traversable $config
148+
* @param int $indentLevel
149+
* @return string
150+
*/
128151
private static function prepareConfig($config, $indentLevel = 1)
129152
{
130153
$indent = str_repeat(' ', $indentLevel * 4);
131154
$entries = [];
132155
foreach ($config as $key => $value) {
133-
$key = class_exists($key)
134-
? sprintf('\\%s::class', $key)
135-
: sprintf("'%s'", $key);
136-
$value = is_array($value) || $value instanceof Traversable
137-
? self::prepareConfig($value, $indentLevel + 1)
138-
: var_export($value, true);
156+
$key = self::createConfigKey($key);
139157
$entries[] = sprintf(
140-
'%s%s => %s,',
158+
'%s%s%s,',
141159
$indent,
142-
$key,
143-
$value
160+
$key ? sprintf('%s => ', $key) : '',
161+
self::createConfigValue($value, $indentLevel)
144162
);
145163
}
146164

@@ -152,4 +170,39 @@ private static function prepareConfig($config, $indentLevel = 1)
152170
$outerIndent
153171
);
154172
}
173+
174+
/**
175+
* @param string|int|null $key
176+
* @return null|string
177+
*/
178+
private static function createConfigKey($key)
179+
{
180+
if (is_string($key) && class_exists($key)) {
181+
return sprintf('\\%s::class', $key);
182+
}
183+
184+
if (is_int($key)) {
185+
return null;
186+
}
187+
188+
return sprintf("'%s'", $key);
189+
}
190+
191+
/**
192+
* @param mixed $value
193+
* @param int $indentLevel
194+
* @return string
195+
*/
196+
private static function createConfigValue($value, $indentLevel)
197+
{
198+
if (is_array($value) || $value instanceof Traversable) {
199+
return self::prepareConfig($value, $indentLevel + 1);
200+
}
201+
202+
if (is_string($value) && class_exists($value)) {
203+
return sprintf('\\%s::class', $value);
204+
}
205+
206+
return var_export($value, true);
207+
}
155208
}

0 commit comments

Comments
 (0)