Skip to content

Commit afdbd1f

Browse files
committed
used sprintf() for error messages
1 parent e14684d commit afdbd1f

20 files changed

+185
-73
lines changed

src/DI/Autowiring.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ public function getByType(string $type, bool $throw = false): ?string
6666
$hint = count($list) === 2 && ($tmp = strpos($list[0], '.') xor strpos($list[1], '.'))
6767
? '. If you want to overwrite service ' . $list[$tmp ? 0 : 1] . ', give it proper name.'
6868
: '';
69-
throw new ServiceCreationException("Multiple services of type $type found: " . implode(', ', $list) . $hint);
69+
throw new ServiceCreationException(sprintf(
70+
"Multiple services of type $type found: %s%s",
71+
implode(', ', $list),
72+
$hint
73+
));
7074
}
7175
}
7276

@@ -123,7 +127,11 @@ public function rebuild(): void
123127
if ($autowiredType === ContainerBuilder::THIS_SERVICE) {
124128
$autowired[$k] = $type;
125129
} elseif (!is_a($type, $autowiredType, true)) {
126-
throw new ServiceCreationException("Incompatible class $autowiredType in autowiring definition of service '$name'.");
130+
throw new ServiceCreationException(sprintf(
131+
"Incompatible class %s in autowiring definition of service '%s'.",
132+
$autowiredType,
133+
$name
134+
));
127135
}
128136
}
129137
}

src/DI/Compiler.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,16 @@ public function addExtension(?string $name, CompilerExtension $extension)
6565
if ($name === null) {
6666
$name = '_' . count($this->extensions);
6767
} elseif (isset($this->extensions[$name])) {
68-
throw new Nette\InvalidArgumentException("Name '$name' is already used or reserved.");
68+
throw new Nette\InvalidArgumentException(sprintf("Name '%s' is already used or reserved.", $name));
6969
}
7070
$lname = strtolower($name);
7171
foreach (array_keys($this->extensions) as $nm) {
7272
if ($lname === strtolower((string) $nm)) {
73-
throw new Nette\InvalidArgumentException("Name of extension '$name' has the same name as '$nm' in a case-insensitive manner.");
73+
throw new Nette\InvalidArgumentException(sprintf(
74+
"Name of extension '%s' has the same name as '%s' in a case-insensitive manner.",
75+
$name,
76+
$nm
77+
));
7478
}
7579
}
7680
$this->extensions[$name] = $extension->setCompiler($this, $name);
@@ -238,13 +242,15 @@ public function processExtensions(): void
238242
}
239243

240244
if ($extra = array_diff_key($this->extensions, $extensions, $first, [self::SERVICES => 1])) {
241-
$extra = implode("', '", array_keys($extra));
242-
throw new Nette\DeprecatedException("Extensions '$extra' were added while container was being compiled.");
245+
throw new Nette\DeprecatedException(sprintf(
246+
"Extensions '%s' were added while container was being compiled.",
247+
implode("', '", array_keys($extra))
248+
));
243249

244250
} elseif ($extra = key(array_diff_key($this->configs, $this->extensions))) {
245251
$hint = Nette\Utils\Helpers::getSuggestion(array_keys($this->extensions), $extra);
246252
throw new InvalidConfigurationException(
247-
"Found section '$extra' in configuration, but corresponding extension is missing"
253+
sprintf("Found section '%s' in configuration, but corresponding extension is missing", $extra)
248254
. ($hint ? ", did you mean '$hint'?" : '.')
249255
);
250256
}

src/DI/CompilerExtension.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,11 @@ public function validateConfig(array $expected, array $config = null, string $na
9090
if ($extra = array_diff_key((array) $config, $expected)) {
9191
$name = $name ? str_replace('.', ' › ', $name) : $this->name;
9292
$hint = Nette\Utils\Helpers::getSuggestion(array_keys($expected), key($extra));
93-
$extra = $hint
94-
? key($extra)
95-
: implode("', '{$name} › ", array_keys($extra));
96-
throw new Nette\DI\InvalidConfigurationException("Unknown configuration option '{$name} › {$extra}'" . ($hint ? ", did you mean '{$name} › {$hint}'?" : '.'));
93+
throw new Nette\DI\InvalidConfigurationException(sprintf(
94+
"Unknown configuration option '%s › %s'",
95+
$name,
96+
$hint ? key($extra) : implode("', '{$name} › ", array_keys($extra))
97+
) . ($hint ? ", did you mean '{$name} › {$hint}'?" : '.'));
9798
}
9899
return Nette\Schema\Helpers::merge($config, $expected);
99100
}

src/DI/Config/Adapters/NeonAdapter.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ public function process(array $arr): array
4242
foreach ($arr as $key => $val) {
4343
if (is_string($key) && substr($key, -1) === self::PREVENT_MERGING_SUFFIX) {
4444
if (!is_array($val) && $val !== null) {
45-
throw new Nette\DI\InvalidConfigurationException("Replacing operator is available only for arrays, item '$key' is not array.");
45+
throw new Nette\DI\InvalidConfigurationException(sprintf(
46+
"Replacing operator is available only for arrays, item '%s' is not array.",
47+
$key
48+
));
4649
}
4750
$key = substr($key, 0, -1);
4851
$val[Helpers::PREVENT_MERGING] = true;

src/DI/Config/DefinitionSchema.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function normalize($def, Context $context)
9595
if (isset($def['class']) && !isset($def['type'])) {
9696
if ($def['class'] instanceof Statement) {
9797
$key = end($context->path);
98-
trigger_error("Service '$key': option 'class' should be changed to 'factory'.", E_USER_DEPRECATED);
98+
trigger_error(sprintf("Service '%s': option 'class' should be changed to 'factory'.", $key), E_USER_DEPRECATED);
9999
$def['factory'] = $def['class'];
100100
unset($def['class']);
101101
} elseif (!isset($def['factory']) && !isset($def['dynamic']) && !isset($def['imported'])) {
@@ -107,7 +107,12 @@ public function normalize($def, Context $context)
107107
foreach (['class' => 'type', 'dynamic' => 'imported'] as $alias => $original) {
108108
if (array_key_exists($alias, $def)) {
109109
if (array_key_exists($original, $def)) {
110-
throw new Nette\DI\InvalidConfigurationException("Options '$alias' and '$original' are aliases, use only '$original'.");
110+
throw new Nette\DI\InvalidConfigurationException(sprintf(
111+
"Options '%s' and '%s' are aliases, use only '%s'.",
112+
$alias,
113+
$original,
114+
$original
115+
));
111116
}
112117
$def[$original] = $def[$alias];
113118
unset($def[$alias]);

src/DI/Config/Loader.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ class Loader
4040
public function load(string $file, ?bool $merge = true): array
4141
{
4242
if (!is_file($file) || !is_readable($file)) {
43-
throw new Nette\FileNotFoundException("File '$file' is missing or is not readable.");
43+
throw new Nette\FileNotFoundException(sprintf("File '%s' is missing or is not readable.", $file));
4444
}
4545

4646
if (isset($this->loadedFiles[$file])) {
47-
throw new Nette\InvalidStateException("Recursive included file '$file'");
47+
throw new Nette\InvalidStateException(sprintf("Recursive included file '%s'", $file));
4848
}
4949
$this->loadedFiles[$file] = true;
5050

@@ -77,7 +77,7 @@ public function load(string $file, ?bool $merge = true): array
7777
public function save(array $data, string $file): void
7878
{
7979
if (file_put_contents($file, $this->getAdapter($file)->dump($data)) === false) {
80-
throw new Nette\IOException("Cannot write file '$file'.");
80+
throw new Nette\IOException(sprintf("Cannot write file '%s'.", $file));
8181
}
8282
}
8383

@@ -118,7 +118,7 @@ private function getAdapter(string $file): Adapter
118118
{
119119
$extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
120120
if (!isset($this->adapters[$extension])) {
121-
throw new Nette\InvalidArgumentException("Unknown file extension '$file'.");
121+
throw new Nette\InvalidArgumentException(sprintf("Unknown file extension '%s'.", $file));
122122
}
123123
return is_object($this->adapters[$extension])
124124
? $this->adapters[$extension]

src/DI/Container.php

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function addService(string $name, $service)
6969
{
7070
$name = $this->aliases[$name] ?? $name;
7171
if (isset($this->instances[$name])) {
72-
throw new Nette\InvalidStateException("Service '$name' already exists.");
72+
throw new Nette\InvalidStateException(sprintf("Service '%s' already exists.", $name));
7373

7474
} elseif (!is_object($service)) {
7575
throw new Nette\InvalidArgumentException(sprintf("Service '%s' must be a object, %s given.", $name, gettype($service)));
@@ -83,7 +83,12 @@ public function addService(string $name, $service)
8383
$this->types[$name] = $type;
8484

8585
} elseif (($expectedType = $this->getServiceType($name)) && !is_a($type, $expectedType, true)) {
86-
throw new Nette\InvalidArgumentException("Service '$name' must be instance of $expectedType, " . ($type ? "$type given." : 'add typehint to closure.'));
86+
throw new Nette\InvalidArgumentException(sprintf(
87+
"Service '%s' must be instance of %s, %s.",
88+
$name,
89+
$expectedType,
90+
$type ? "$type given" : 'add typehint to closure'
91+
));
8792
}
8893

8994
if ($service instanceof \Closure) {
@@ -153,7 +158,7 @@ public function getServiceType(string $name): string
153158
return $type ? $type->getName() : '';
154159

155160
} else {
156-
throw new MissingServiceException("Service '$name' not found.");
161+
throw new MissingServiceException(sprintf("Service '%s' not found.", $name));
157162
}
158163
}
159164

@@ -174,7 +179,7 @@ public function hasService(string $name): bool
174179
public function isCreated(string $name): bool
175180
{
176181
if (!$this->hasService($name)) {
177-
throw new MissingServiceException("Service '$name' not found.");
182+
throw new MissingServiceException(sprintf("Service '%s' not found.", $name));
178183
}
179184
$name = $this->aliases[$name] ?? $name;
180185
return isset($this->instances[$name]);
@@ -195,7 +200,7 @@ public function createService(string $name, array $args = [])
195200
throw new Nette\InvalidStateException(sprintf('Circular reference detected for services: %s.', implode(', ', array_keys($this->creating))));
196201

197202
} elseif ($cb === null) {
198-
throw new MissingServiceException("Service '$name' not found.");
203+
throw new MissingServiceException(sprintf("Service '%s' not found.", $name));
199204
}
200205

201206
try {
@@ -209,7 +214,10 @@ public function createService(string $name, array $args = [])
209214
}
210215

211216
if (!is_object($service)) {
212-
throw new Nette\UnexpectedValueException("Unable to create service '$name', value returned by " . ($cb instanceof \Closure ? 'closure' : "method $method()") . ' is not object.');
217+
throw new Nette\UnexpectedValueException(sprintf(
218+
"Unable to create service '$name', value returned by %s is not object.",
219+
$cb instanceof \Closure ? 'closure' : "method $method()"
220+
));
213221
}
214222

215223
return $service;
@@ -230,7 +238,7 @@ public function getByType(string $type, bool $throw = true)
230238
return $this->getService($names[0]);
231239
}
232240
natsort($names);
233-
throw new MissingServiceException("Multiple services of type $type found: " . implode(', ', $names) . '.');
241+
throw new MissingServiceException(sprintf("Multiple services of type $type found: %s.", implode(', ', $names)));
234242

235243
} elseif ($throw) {
236244
if (!class_exists($type) && !interface_exists($type)) {
@@ -239,10 +247,16 @@ public function getByType(string $type, bool $throw = true)
239247
foreach ($this->methods as $method => $foo) {
240248
$methodType = (new \ReflectionMethod(static::class, $method))->getReturnType()->getName();
241249
if (is_a($methodType, $type, true)) {
242-
throw new MissingServiceException("Service of type $type is not autowired or is missing in di › export › types.");
250+
throw new MissingServiceException(sprintf(
251+
'Service of type %s is not autowired or is missing in di › export › types.',
252+
$type
253+
));
243254
}
244255
}
245-
throw new MissingServiceException("Service of type $type not found. Did you add it to configuration file?");
256+
throw new MissingServiceException(sprintf(
257+
'Service of type %s not found. Did you add it to configuration file?',
258+
$type
259+
));
246260
}
247261
return null;
248262
}
@@ -295,13 +309,13 @@ public function createInstance(string $class, array $args = [])
295309
{
296310
$rc = new \ReflectionClass($class);
297311
if (!$rc->isInstantiable()) {
298-
throw new ServiceCreationException("Class $class is not instantiable.");
312+
throw new ServiceCreationException(sprintf('Class %s is not instantiable.', $class));
299313

300314
} elseif ($constructor = $rc->getConstructor()) {
301315
return $rc->newInstanceArgs($this->autowireArguments($constructor, $args));
302316

303317
} elseif ($args) {
304-
throw new ServiceCreationException("Unable to pass arguments, class $class has no constructor.");
318+
throw new ServiceCreationException(sprintf('Unable to pass arguments, class %s has no constructor.', $class));
305319
}
306320
return new $class;
307321
}

src/DI/ContainerBuilder.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,16 @@ public function addDefinition(?string $name, Definition $definition = null): Def
7474
} else {
7575
$name = $this->aliases[$name] ?? $name;
7676
if (isset($this->definitions[$name])) {
77-
throw new Nette\InvalidStateException("Service '$name' has already been added.");
77+
throw new Nette\InvalidStateException(sprintf("Service '%s' has already been added.", $name));
7878
}
7979
$lname = strtolower($name);
8080
foreach ($this->definitions as $nm => $foo) {
8181
if ($lname === strtolower($nm)) {
82-
throw new Nette\InvalidStateException("Service '$name' has the same name as '$nm' in a case-insensitive manner.");
82+
throw new Nette\InvalidStateException(sprintf(
83+
"Service '%s' has the same name as '%s' in a case-insensitive manner.",
84+
$name,
85+
$nm
86+
));
8387
}
8488
}
8589
}
@@ -135,7 +139,7 @@ public function getDefinition(string $name): Definition
135139
{
136140
$service = $this->aliases[$name] ?? $name;
137141
if (!isset($this->definitions[$service])) {
138-
throw new MissingServiceException("Service '$name' not found.");
142+
throw new MissingServiceException(sprintf("Service '%s' not found.", $name));
139143
}
140144
return $this->definitions[$service];
141145
}
@@ -170,10 +174,10 @@ public function addAlias(string $alias, string $service): void
170174
throw new Nette\InvalidArgumentException(sprintf('Service name must be a non-empty string, %s given.', gettype($service)));
171175

172176
} elseif (isset($this->aliases[$alias])) {
173-
throw new Nette\InvalidStateException("Alias '$alias' has already been added.");
177+
throw new Nette\InvalidStateException(sprintf("Alias '%s' has already been added.", $alias));
174178

175179
} elseif (isset($this->definitions[$alias])) {
176-
throw new Nette\InvalidStateException("Service '$alias' has already been added.");
180+
throw new Nette\InvalidStateException(sprintf("Service '%s' has already been added.", $alias));
177181
}
178182
$this->aliases[$alias] = $service;
179183
}

src/DI/ContainerLoader.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ private function loadFile(string $class, callable $generator): void
6767

6868
$handle = @fopen("$file.lock", 'c+'); // @ is escalated to exception
6969
if (!$handle) {
70-
throw new Nette\IOException("Unable to create file '$file.lock'. " . Nette\Utils\Helpers::getLastError());
70+
throw new Nette\IOException(sprintf("Unable to create file '%s.lock'. %s", $file, Nette\Utils\Helpers::getLastError()));
7171
} elseif (!@flock($handle, LOCK_EX)) { // @ is escalated to exception
72-
throw new Nette\IOException("Unable to acquire exclusive lock on '$file.lock'. " . Nette\Utils\Helpers::getLastError());
72+
throw new Nette\IOException(sprintf("Unable to acquire exclusive lock on '%s.lock'. %s", $file, Nette\Utils\Helpers::getLastError()));
7373
}
7474

7575
if (!is_file($file) || $this->isExpired($file, $updatedMeta)) {
@@ -82,15 +82,15 @@ private function loadFile(string $class, callable $generator): void
8282
foreach ($toWrite as $name => $content) {
8383
if (file_put_contents("$name.tmp", $content) !== strlen($content) || !rename("$name.tmp", $name)) {
8484
@unlink("$name.tmp"); // @ - file may not exist
85-
throw new Nette\IOException("Unable to create file '$name'.");
85+
throw new Nette\IOException(sprintf("Unable to create file '%s'.", $name));
8686
} elseif (function_exists('opcache_invalidate')) {
8787
@opcache_invalidate($name, true); // @ can be restricted
8888
}
8989
}
9090
}
9191

9292
if ((@include $file) === false) { // @ - error escalated to exception
93-
throw new Nette\IOException("Unable to include '$file'.");
93+
throw new Nette\IOException(sprintf("Unable to include '%s'.", $file));
9494
}
9595
flock($handle, LOCK_UN);
9696
}

src/DI/Definitions/AccessorDefinition.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ final class AccessorDefinition extends Definition
2929
public function setImplement(string $type)
3030
{
3131
if (!interface_exists($type)) {
32-
throw new Nette\InvalidArgumentException("Service '{$this->getName()}': Interface '$type' not found.");
32+
throw new Nette\InvalidArgumentException(sprintf("Service '%s': Interface '%s' not found.", $this->getName(), $type));
3333
}
3434
$rc = new \ReflectionClass($type);
3535

@@ -40,9 +40,17 @@ public function setImplement(string $type)
4040
|| $method->getName() !== self::METHOD_GET
4141
|| count($rc->getMethods()) > 1
4242
) {
43-
throw new Nette\InvalidArgumentException("Service '{$this->getName()}': Interface $type must have just one non-static method get().");
43+
throw new Nette\InvalidArgumentException(sprintf(
44+
"Service '%s': Interface %s must have just one non-static method get().",
45+
$this->getName(),
46+
$type
47+
));
4448
} elseif ($method->getNumberOfParameters()) {
45-
throw new Nette\InvalidArgumentException("Service '{$this->getName()}': Method $type::get() must have no parameters.");
49+
throw new Nette\InvalidArgumentException(sprintf(
50+
"Service '%s': Method %s::get() must have no parameters.",
51+
$this->getName(),
52+
$type
53+
));
4654
}
4755
return parent::setType($type);
4856
}

0 commit comments

Comments
 (0)