Skip to content

Commit 5860223

Browse files
committed
refactoring: renamed some variables $class to $type
1 parent 1705a5d commit 5860223

File tree

7 files changed

+85
-85
lines changed

7 files changed

+85
-85
lines changed

src/Bridges/DITracy/templates/ContainerPanel.panel.phtml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ use Tracy\Helpers;
4646
</tr>
4747
</thead>
4848
<tbody>
49-
<?php foreach ($services as $name => $class): ?>
49+
<?php foreach ($services as $name => $type): ?>
5050
<?php $name = (string) $name ?>
51-
<?php $autowired = !empty($meta[Nette\DI\Container::TYPES][$class][true]) && in_array($name, $meta[Nette\DI\Container::TYPES][$class][true], true) ?>
51+
<?php $autowired = !empty($meta[Nette\DI\Container::TYPES][$type][true]) && in_array($name, $meta[Nette\DI\Container::TYPES][$type][true], true) ?>
5252
<tr>
5353
<td class="<?= isset($registry[$name]) ? 'created' : '' ?>"><?= htmlspecialchars($name) ?></td>
5454
<td class="<?= $autowired ? 'yes' : '' ?>"><?= $autowired ? 'yes' : 'no' ?></td>
@@ -57,8 +57,8 @@ use Tracy\Helpers;
5757
<?= Dumper::toHtml($registry[$name], [Dumper::COLLAPSE => true, Dumper::LIVE => true]); ?>
5858
<?php elseif (isset($registry[$name])): ?>
5959
<code><?= get_class($registry[$name]) ?></code>
60-
<?php elseif (is_string($class)): ?>
61-
<code><?= htmlspecialchars($class) ?></code>
60+
<?php elseif (is_string($type)): ?>
61+
<code><?= htmlspecialchars($type) ?></code>
6262
<?php endif ?>
6363
</td>
6464
<td><?php if (isset($tags[$name])) { echo Dumper::toHtml($tags[$name], [Dumper::COLLAPSE => true]); } ?></td>

src/DI/Container.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -188,17 +188,17 @@ public function createService(string $name, array $args = [])
188188
* @return object service or null
189189
* @throws MissingServiceException
190190
*/
191-
public function getByType(string $class, bool $throw = true)
191+
public function getByType(string $type, bool $throw = true)
192192
{
193-
$class = Helpers::normalizeClass($class);
194-
if (!empty($this->meta[self::TYPES][$class][true])) {
195-
if (count($names = $this->meta[self::TYPES][$class][true]) === 1) {
193+
$type = Helpers::normalizeClass($type);
194+
if (!empty($this->meta[self::TYPES][$type][true])) {
195+
if (count($names = $this->meta[self::TYPES][$type][true]) === 1) {
196196
return $this->getService($names[0]);
197197
}
198-
throw new MissingServiceException("Multiple services of type $class found: " . implode(', ', $names) . '.');
198+
throw new MissingServiceException("Multiple services of type $type found: " . implode(', ', $names) . '.');
199199

200200
} elseif ($throw) {
201-
throw new MissingServiceException("Service of type $class not found.");
201+
throw new MissingServiceException("Service of type $type not found.");
202202
}
203203
}
204204

@@ -207,12 +207,12 @@ public function getByType(string $class, bool $throw = true)
207207
* Gets the service names of the specified type.
208208
* @return string[]
209209
*/
210-
public function findByType(string $class): array
210+
public function findByType(string $type): array
211211
{
212-
$class = Helpers::normalizeClass($class);
213-
return empty($this->meta[self::TYPES][$class])
212+
$type = Helpers::normalizeClass($type);
213+
return empty($this->meta[self::TYPES][$type])
214214
? []
215-
: array_merge(...array_values($this->meta[self::TYPES][$class]));
215+
: array_merge(...array_values($this->meta[self::TYPES][$type]));
216216
}
217217

218218

src/DI/ContainerBuilder.php

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,12 @@ public function getAliases(): array
161161
* @param string[]
162162
* @return static
163163
*/
164-
public function addExcludedClasses(array $classes)
164+
public function addExcludedClasses(array $types)
165165
{
166-
foreach ($classes as $class) {
167-
if (class_exists($class) || interface_exists($class)) {
168-
$class = Helpers::normalizeClass($class);
169-
$this->excludedClasses += class_parents($class) + class_implements($class) + [$class => $class];
166+
foreach ($types as $type) {
167+
if (class_exists($type) || interface_exists($type)) {
168+
$type = Helpers::normalizeClass($type);
169+
$this->excludedClasses += class_parents($type) + class_implements($type) + [$type => $type];
170170
}
171171
}
172172
return $this;
@@ -183,56 +183,56 @@ public function addExcludedClasses(array $classes)
183183
* @return string|null service name or null
184184
* @throws ServiceCreationException
185185
*/
186-
public function getByType(string $class, bool $throw = false)
186+
public function getByType(string $type, bool $throw = false)
187187
{
188-
$class = Helpers::normalizeClass($class);
188+
$type = Helpers::normalizeClass($type);
189189

190190
if ($this->currentService !== null
191-
&& is_a($this->definitions[$this->currentService]->getType(), $class, true)
191+
&& is_a($this->definitions[$this->currentService]->getType(), $type, true)
192192
) {
193193
return $this->currentService;
194194
}
195195

196-
$classes = $this->getClassList();
197-
if (empty($classes[$class][true])) {
196+
$types = $this->getClassList();
197+
if (empty($types[$type][true])) {
198198
if ($throw) {
199-
throw new MissingServiceException("Service of type '$class' not found.");
199+
throw new MissingServiceException("Service of type '$type' not found.");
200200
}
201201
return;
202202

203-
} elseif (count($classes[$class][true]) === 1) {
204-
return $classes[$class][true][0];
203+
} elseif (count($types[$type][true]) === 1) {
204+
return $types[$type][true][0];
205205

206206
} else {
207-
$list = $classes[$class][true];
207+
$list = $types[$type][true];
208208
$hint = count($list) === 2 && ($tmp = strpos($list[0], '.') xor strpos($list[1], '.'))
209209
? '. If you want to overwrite service ' . $list[$tmp ? 0 : 1] . ', give it proper name.'
210210
: '';
211-
throw new ServiceCreationException("Multiple services of type $class found: " . implode(', ', $list) . $hint);
211+
throw new ServiceCreationException("Multiple services of type $type found: " . implode(', ', $list) . $hint);
212212
}
213213
}
214214

215215

216216
/**
217217
* Gets the service definition of the specified type.
218218
*/
219-
public function getDefinitionByType(string $class): ServiceDefinition
219+
public function getDefinitionByType(string $type): ServiceDefinition
220220
{
221-
return $this->getDefinition($this->getByType($class, true));
221+
return $this->getDefinition($this->getByType($type, true));
222222
}
223223

224224

225225
/**
226226
* Gets the service names and definitions of the specified type.
227227
* @return ServiceDefinition[]
228228
*/
229-
public function findByType(string $class): array
229+
public function findByType(string $type): array
230230
{
231-
$class = Helpers::normalizeClass($class);
231+
$type = Helpers::normalizeClass($type);
232232
$found = [];
233-
$classes = $this->getClassList();
234-
if (!empty($classes[$class])) {
235-
foreach (array_merge(...array_values($classes[$class])) as $name) {
233+
$types = $this->getClassList();
234+
if (!empty($types[$type])) {
235+
foreach (array_merge(...array_values($types[$type])) as $name) {
236236
$found[$name] = $this->definitions[$name];
237237
}
238238
}
@@ -270,7 +270,7 @@ public function getClassList(): array
270270

271271

272272
/**
273-
* Generates $dependencies, $classes and normalizes class names.
273+
* Generates $dependencies, $classList and normalizes class names.
274274
* @return void
275275
* @internal
276276
*/
@@ -314,30 +314,30 @@ public function prepareClassList()
314314

315315
// resolve and check classes
316316
foreach ($this->definitions as $name => $def) {
317-
$this->resolveServiceClass($name);
317+
$this->resolveServiceType($name);
318318
}
319319

320320
// build auto-wiring list
321321
$this->classList = $preferred = [];
322322
foreach ($this->definitions as $name => $def) {
323-
if ($class = $def->getImplement() ?: $def->getType()) {
323+
if ($type = $def->getImplement() ?: $def->getType()) {
324324
$defAutowired = $def->getAutowired();
325325
if (is_array($defAutowired)) {
326-
foreach ($defAutowired as $k => $aclass) {
327-
if ($aclass === self::THIS_SERVICE) {
328-
$defAutowired[$k] = $class;
329-
} elseif (!is_a($class, $aclass, true)) {
330-
throw new ServiceCreationException("Incompatible class $aclass in autowiring definition of service '$name'.");
326+
foreach ($defAutowired as $k => $autowiredType) {
327+
if ($autowiredType === self::THIS_SERVICE) {
328+
$defAutowired[$k] = $type;
329+
} elseif (!is_a($type, $autowiredType, true)) {
330+
throw new ServiceCreationException("Incompatible class $autowiredType in autowiring definition of service '$name'.");
331331
}
332332
}
333333
}
334334

335-
foreach (class_parents($class) + class_implements($class) + [$class] as $parent) {
335+
foreach (class_parents($type) + class_implements($type) + [$type] as $parent) {
336336
$autowired = $defAutowired && empty($this->excludedClasses[$parent]);
337337
if ($autowired && is_array($defAutowired)) {
338338
$autowired = false;
339-
foreach ($defAutowired as $aclass) {
340-
if (is_a($parent, $aclass, true)) {
339+
foreach ($defAutowired as $autowiredType) {
340+
if (is_a($parent, $autowiredType, true)) {
341341
if (empty($preferred[$parent]) && isset($this->classList[$parent][true])) {
342342
$this->classList[$parent][false] = array_merge(...$this->classList[$parent]);
343343
$this->classList[$parent][true] = [];
@@ -403,7 +403,7 @@ private function resolveImplement(ServiceDefinition $def, $name)
403403
if (!$def->getEntity()) {
404404
$def->setFactory($def->getType(), $def->getFactory() ? $def->getFactory()->arguments : []);
405405
}
406-
if (($class = $this->resolveEntityClass($def->getFactory(), [$name => 1]))
406+
if (($class = $this->resolveEntityType($def->getFactory(), [$name => 1]))
407407
&& ($ctor = (new ReflectionClass($class))->getConstructor())
408408
) {
409409
foreach ($ctor->getParameters() as $param) {
@@ -436,45 +436,45 @@ private function resolveImplement(ServiceDefinition $def, $name)
436436

437437

438438
/** @return string|null */
439-
private function resolveServiceClass($name, array $recursive = [])
439+
private function resolveServiceType($name, array $recursive = [])
440440
{
441441
if (isset($recursive[$name])) {
442442
throw new ServiceCreationException(sprintf('Circular reference detected for services: %s.', implode(', ', array_keys($recursive))));
443443
}
444444
$recursive[$name] = true;
445445

446446
$def = $this->definitions[$name];
447-
$factoryClass = $def->getFactory() ? $this->resolveEntityClass($def->getFactory()->getEntity(), $recursive) : null; // call always to check entities
448-
if ($class = $def->getType() ?: $factoryClass) {
449-
if (!class_exists($class) && !interface_exists($class)) {
450-
throw new ServiceCreationException("Class or interface '$class' used in service '$name' not found.");
447+
$factoryClass = $def->getFactory() ? $this->resolveEntityType($def->getFactory()->getEntity(), $recursive) : null; // call always to check entities
448+
if ($type = $def->getType() ?: $factoryClass) {
449+
if (!class_exists($type) && !interface_exists($type)) {
450+
throw new ServiceCreationException("Class or interface '$type' used in service '$name' not found.");
451451
}
452-
$class = Helpers::normalizeClass($class);
453-
$def->setType($class);
452+
$type = Helpers::normalizeClass($type);
453+
$def->setType($type);
454454
if (count($recursive) === 1) {
455-
$this->addDependency(new ReflectionClass($factoryClass ?: $class));
455+
$this->addDependency(new ReflectionClass($factoryClass ?: $type));
456456
}
457457

458458
} elseif ($def->getAutowired()) {
459459
throw new ServiceCreationException("Unknown type of service '$name', declare return type of factory method (for PHP 5 use annotation @return)");
460460
}
461-
return $class;
461+
return $type;
462462
}
463463

464464

465465
/** @return string|null */
466-
private function resolveEntityClass($entity, array $recursive = [])
466+
private function resolveEntityType($entity, array $recursive = [])
467467
{
468468
$entity = $this->normalizeEntity($entity instanceof Statement ? $entity->getEntity() : $entity);
469469
$serviceName = current(array_slice(array_keys($recursive), -1));
470470

471471
if (is_array($entity)) {
472472
if (($service = $this->getServiceName($entity[0])) || $entity[0] instanceof Statement) {
473-
$entity[0] = $this->resolveEntityClass($entity[0], $recursive);
473+
$entity[0] = $this->resolveEntityType($entity[0], $recursive);
474474
if (!$entity[0]) {
475475
return;
476476
} elseif (isset($this->definitions[$service]) && $this->definitions[$service]->getImplement()) { // @Implement::create
477-
return $entity[1] === 'create' ? $this->resolveServiceClass($service, $recursive) : null;
477+
return $entity[1] === 'create' ? $this->resolveServiceType($service, $recursive) : null;
478478
}
479479
}
480480

@@ -503,7 +503,7 @@ private function resolveEntityClass($entity, array $recursive = [])
503503
}
504504
return $this->definitions[$service]->getImplement()
505505
?: $this->definitions[$service]->getType()
506-
?: $this->resolveServiceClass($service, $recursive);
506+
?: $this->resolveServiceType($service, $recursive);
507507

508508
} elseif (is_string($entity)) { // class
509509
if (!class_exists($entity)) {
@@ -619,11 +619,11 @@ public function completeStatement(Statement $statement): Statement
619619
if (!$arguments && substr($entity[1], -2) === '[]') {
620620
throw new ServiceCreationException("Missing argument for $entity[1].");
621621
}
622-
} elseif ($class = empty($service) || $entity[1] === 'create'
623-
? $this->resolveEntityClass($entity[0])
622+
} elseif ($type = empty($service) || $entity[1] === 'create'
623+
? $this->resolveEntityType($entity[0])
624624
: $this->definitions[$service]->getType()
625625
) {
626-
$arguments = $this->autowireArguments($class, $entity[1], $arguments);
626+
$arguments = $this->autowireArguments($type, $entity[1], $arguments);
627627
}
628628
}
629629

src/DI/Extensions/DecoratorExtension.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ final class DecoratorExtension extends Nette\DI\CompilerExtension
2626

2727
public function beforeCompile()
2828
{
29-
foreach ($this->getConfig() as $class => $info) {
30-
$info = $this->validateConfig($this->defaults, $info, $this->prefix($class));
29+
foreach ($this->getConfig() as $type => $info) {
30+
$info = $this->validateConfig($this->defaults, $info, $this->prefix($type));
3131
if ($info['inject'] !== null) {
3232
$info['tags'][InjectExtension::TAG_INJECT] = $info['inject'];
3333
}
3434
$info = Nette\DI\Helpers::filterArguments($info);
35-
$this->addSetups($class, (array) $info['setup']);
36-
$this->addTags($class, (array) $info['tags']);
35+
$this->addSetups($type, (array) $info['setup']);
36+
$this->addTags($type, (array) $info['tags']);
3737
}
3838
}
3939

src/DI/Helpers.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,15 @@ public static function autowireArguments(\ReflectionFunctionAbstract $method, ar
108108
unset($arguments[$num]);
109109
$optCount = 0;
110110

111-
} elseif (($class = Reflection::getParameterType($parameter)) && !Reflection::isBuiltinType($class)) {
112-
$res[$num] = $container->getByType($class, false);
111+
} elseif (($type = Reflection::getParameterType($parameter)) && !Reflection::isBuiltinType($type)) {
112+
$res[$num] = $container->getByType($type, false);
113113
if ($res[$num] === null) {
114114
if ($parameter->allowsNull()) {
115115
$optCount++;
116-
} elseif (class_exists($class) || interface_exists($class)) {
117-
throw new ServiceCreationException("Service of type $class needed by $methodName not found. Did you register it in configuration file?");
116+
} elseif (class_exists($type) || interface_exists($type)) {
117+
throw new ServiceCreationException("Service of type $type needed by $methodName not found. Did you register it in configuration file?");
118118
} else {
119-
throw new ServiceCreationException("Class $class needed by $methodName not found. Check type hint and 'use' statements.");
119+
throw new ServiceCreationException("Class $type needed by $methodName not found. Check type hint and 'use' statements.");
120120
}
121121
} else {
122122
if ($container instanceof ContainerBuilder) {
@@ -125,7 +125,7 @@ public static function autowireArguments(\ReflectionFunctionAbstract $method, ar
125125
$optCount = 0;
126126
}
127127

128-
} elseif (($class && $parameter->allowsNull()) || $parameter->isOptional() || $parameter->isDefaultValueAvailable()) {
128+
} elseif (($type && $parameter->allowsNull()) || $parameter->isOptional() || $parameter->isDefaultValueAvailable()) {
129129
// !optional + defaultAvailable = func($a = null, $b) since 5.4.7
130130
// optional + !defaultAvailable = i.e. Exception::__construct, mysqli::mysqli, ...
131131
$res[$num] = $parameter->isDefaultValueAvailable() ? Reflection::getParameterDefaultValue($parameter) : null;
@@ -233,10 +233,10 @@ public static function getReturnType(\ReflectionFunctionAbstract $func)
233233
}
234234

235235

236-
public static function normalizeClass(string $class): string
236+
public static function normalizeClass(string $type): string
237237
{
238-
return class_exists($class) || interface_exists($class)
239-
? (new \ReflectionClass($class))->getName()
240-
: $class;
238+
return class_exists($type) || interface_exists($type)
239+
? (new \ReflectionClass($type))->getName()
240+
: $type;
241241
}
242242
}

src/DI/PhpGenerator.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,12 @@ private function generateService(string $name): string
118118
$this->currentService = null;
119119
$code = '$service = ' . $this->formatStatement($factory) . ";\n";
120120

121-
if ($def->getSetup() && ($class = $def->getType()) && !$serviceRef && $class !== $entity
122-
&& !(is_string($entity) && preg_match('#^[\w\\\\]+\z#', $entity) && is_subclass_of($entity, $class))
121+
if ($def->getSetup() && ($type = $def->getType()) && !$serviceRef && $type !== $entity
122+
&& !(is_string($entity) && preg_match('#^[\w\\\\]+\z#', $entity) && is_subclass_of($entity, $type))
123123
) {
124-
$code .= PhpHelpers::formatArgs("if (!\$service instanceof $class) {\n"
124+
$code .= PhpHelpers::formatArgs("if (!\$service instanceof $type) {\n"
125125
. "\tthrow new Nette\\UnexpectedValueException(?);\n}\n",
126-
["Unable to create service '$name', value returned by factory is not $class type."]
126+
["Unable to create service '$name', value returned by factory is not $type type."]
127127
);
128128
}
129129

src/DI/ServiceDefinition.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ final class ServiceDefinition
6363
* @return static
6464
* @deprecated
6565
*/
66-
public function setClass($class)
66+
public function setClass($type)
6767
{
6868
($this->notifier)();
69-
$this->type = $class;
69+
$this->type = $type;
7070
if (func_num_args() > 1) {
7171
trigger_error(__METHOD__ . '() second parameter $args is deprecated, use setFactory()', E_USER_DEPRECATED);
7272
if ($args = func_get_arg(1)) {
73-
$this->setFactory($class, $args);
73+
$this->setFactory($type, $args);
7474
}
7575
}
7676
return $this;

0 commit comments

Comments
 (0)