From 91ef29c00fdeb2e2ca94040225f2a5695de7ade8 Mon Sep 17 00:00:00 2001 From: "hubert.lenoir" Date: Thu, 8 Aug 2024 15:06:41 +0200 Subject: [PATCH] feat: use optimized sprintf --- src/Asserter.php | 2 +- src/Context/DebugContext.php | 2 +- src/Context/JsonContext.php | 18 +++++++++--------- src/Context/RestContext.php | 4 ++-- src/Context/SystemContext.php | 14 +++++++------- src/Json/JsonSchema.php | 2 +- 6 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/Asserter.php b/src/Asserter.php index 0aa5400d..80c7580f 100644 --- a/src/Asserter.php +++ b/src/Asserter.php @@ -47,7 +47,7 @@ protected function assertCount($expected, array $elements, $message = null): voi { $this->assert( (int) $expected === \count($elements), - $message ?: sprintf('%d elements found, but should be %d.', \count($elements), $expected) + $message ?: \sprintf('%d elements found, but should be %d.', \count($elements), $expected) ); } diff --git a/src/Context/DebugContext.php b/src/Context/DebugContext.php index c707d5ac..d13e62e4 100644 --- a/src/Context/DebugContext.php +++ b/src/Context/DebugContext.php @@ -63,7 +63,7 @@ public function failScreenshots(AfterStepScope $scope): void $scenarioName = urlencode(str_replace(' ', '_', $scenario->getTitle() ?? '')); } - $filename = sprintf('fail_%s_%s_%s_%s.png', time(), $suiteName, $featureName, $scenarioName); + $filename = \sprintf('fail_%s_%s_%s_%s.png', time(), $suiteName, $featureName, $scenarioName); $this->saveScreenshot($filename, $this->screenshotDir); } diff --git a/src/Context/JsonContext.php b/src/Context/JsonContext.php index f9a13b56..c808c32d 100644 --- a/src/Context/JsonContext.php +++ b/src/Context/JsonContext.php @@ -55,7 +55,7 @@ public function theJsonNodeShouldBeEqualTo($node, $expected): void $actual = $this->inspector->evaluate($json, $node); if ($actual != $expected) { - throw new \Exception(sprintf("The node '%s' value is '%s', '%s' expected", $node, json_encode($actual), $expected)); + throw new \Exception(\sprintf("The node '%s' value is '%s', '%s' expected", $node, json_encode($actual), $expected)); } } @@ -75,7 +75,7 @@ public function theJsonNodesShouldBeEqualTo(TableNode $nodes): void $expected = self::reespaceSpecialGherkinValue($expected); if ($actual != $expected) { - $errors[] = sprintf("The node '%s' value is '%s', '%s' expected", $node, json_encode($actual), $expected); + $errors[] = \sprintf("The node '%s' value is '%s', '%s' expected", $node, json_encode($actual), $expected); } } @@ -96,7 +96,7 @@ public function theJsonNodeShouldMatch($node, $pattern): void $actual = $this->inspector->evaluate($json, $node); if (0 === preg_match($pattern, $actual)) { - throw new \Exception(sprintf("The node '%s' value is '%s', '%s' pattern expected", $node, json_encode($actual), $pattern)); + throw new \Exception(\sprintf("The node '%s' value is '%s', '%s' pattern expected", $node, json_encode($actual), $pattern)); } } @@ -112,7 +112,7 @@ public function theJsonNodeShouldBeNull($node): void $actual = $this->inspector->evaluate($json, $node); if (null !== $actual) { - throw new \Exception(sprintf("The node '%s' value is '%s', null expected", $node, json_encode($actual))); + throw new \Exception(\sprintf("The node '%s' value is '%s', null expected", $node, json_encode($actual))); } } @@ -128,7 +128,7 @@ public function theJsonNodeShouldNotBeNull($node): void $actual = $this->inspector->evaluate($json, $node); if (null === $actual) { - throw new \Exception(sprintf("The node '%s' value is null, non-null value expected", $node)); + throw new \Exception(\sprintf("The node '%s' value is null, non-null value expected", $node)); } } @@ -144,7 +144,7 @@ public function theJsonNodeShouldBeTrue($node): void $actual = $this->inspector->evaluate($json, $node); if (true !== $actual) { - throw new \Exception(sprintf("The node '%s' value is '%s', 'true' expected", $node, json_encode($actual))); + throw new \Exception(\sprintf("The node '%s' value is '%s', 'true' expected", $node, json_encode($actual))); } } @@ -160,7 +160,7 @@ public function theJsonNodeShouldBeFalse($node): void $actual = $this->inspector->evaluate($json, $node); if (false !== $actual) { - throw new \Exception(sprintf("The node '%s' value is '%s', 'false' expected", $node, json_encode($actual))); + throw new \Exception(\sprintf("The node '%s' value is '%s', 'false' expected", $node, json_encode($actual))); } } @@ -176,7 +176,7 @@ public function theJsonNodeShouldBeEqualToTheString($node, $expected): void $actual = $this->inspector->evaluate($json, $node); if ($actual !== $expected) { - throw new \Exception(sprintf("The node '%s' value is '%s', string '%s' expected", $node, json_encode($actual), $expected)); + throw new \Exception(\sprintf("The node '%s' value is '%s', string '%s' expected", $node, json_encode($actual), $expected)); } } @@ -192,7 +192,7 @@ public function theJsonNodeShouldBeEqualToTheNumber($node, $number): void $actual = $this->inspector->evaluate($json, $node); if ($actual !== (float) $number && $actual !== (int) $number) { - throw new \Exception(sprintf("The node '%s' value is '%s', number '%s' expected", $node, json_encode($actual), (string) $number)); + throw new \Exception(\sprintf("The node '%s' value is '%s', number '%s' expected", $node, json_encode($actual), (string) $number)); } } diff --git a/src/Context/RestContext.php b/src/Context/RestContext.php index 38fb9f6c..1665b97d 100644 --- a/src/Context/RestContext.php +++ b/src/Context/RestContext.php @@ -130,7 +130,7 @@ public function theHeaderShouldNotBeEqualTo($name, $value): void public function theHeaderShouldBeContains($name, $value): void { @trigger_error( - sprintf('The %s function is deprecated since version 3.1 and will be removed in 4.0. Use the %s::theHeaderShouldContain function instead.', __METHOD__, __CLASS__), + \sprintf('The %s function is deprecated since version 3.1 and will be removed in 4.0. Use the %s::theHeaderShouldContain function instead.', __METHOD__, __CLASS__), \E_USER_DEPRECATED ); $this->theHeaderShouldContain($name, $value); @@ -216,7 +216,7 @@ public function theResponseShouldExpireInTheFuture(): void $expires = new \DateTime($this->request->getHttpRawHeader('Expires')[0]); $this->assertSame(1, $expires->diff($date)->invert, - sprintf('The response doesn\'t expire in the future (%s)', $expires->format(\DATE_ATOM)) + \sprintf('The response doesn\'t expire in the future (%s)', $expires->format(\DATE_ATOM)) ); } diff --git a/src/Context/SystemContext.php b/src/Context/SystemContext.php index f4eebbff..2066352b 100644 --- a/src/Context/SystemContext.php +++ b/src/Context/SystemContext.php @@ -66,7 +66,7 @@ public function iDumpCommandOutput(): void public function commandShouldSucceed(): void { if (0 !== $this->lastReturnCode) { - throw new \Exception(sprintf('Command should succeed %b', $this->lastReturnCode)); + throw new \Exception(\sprintf('Command should succeed %b', $this->lastReturnCode)); } } @@ -78,7 +78,7 @@ public function commandShouldSucceed(): void public function commandShouldFail(): void { if (0 === $this->lastReturnCode) { - throw new \Exception(sprintf('Command should fail %b', $this->lastReturnCode)); + throw new \Exception(\sprintf('Command should fail %b', $this->lastReturnCode)); } } @@ -90,7 +90,7 @@ public function commandShouldFail(): void public function commandShouldLastLessThan($seconds): void { if ($this->lastExecutionTime > $seconds) { - throw new \Exception(sprintf('Last command last %s which is more than %s seconds', $this->lastExecutionTime, $seconds)); + throw new \Exception(\sprintf('Last command last %s which is more than %s seconds', $this->lastExecutionTime, $seconds)); } } @@ -102,7 +102,7 @@ public function commandShouldLastLessThan($seconds): void public function commandShouldMoreLessThan($seconds): void { if ($this->lastExecutionTime < $seconds) { - throw new \Exception(sprintf('Last command last %s which is less than %s seconds', $this->lastExecutionTime, $seconds)); + throw new \Exception(\sprintf('Last command last %s which is less than %s seconds', $this->lastExecutionTime, $seconds)); } } @@ -124,7 +124,7 @@ public function outputShouldContain($text): void } if (false === $check) { - throw new \Exception(sprintf("The text '%s' was not found anywhere on output of command.\n%s", $text, implode("\n", $this->output))); + throw new \Exception(\sprintf("The text '%s' was not found anywhere on output of command.\n%s", $text, implode("\n", $this->output))); } } @@ -139,7 +139,7 @@ public function outputShouldNotContain($text): void foreach ($this->output as $line) { if (1 === preg_match($regex, $line)) { - throw new \Exception(sprintf("The text '%s' was found somewhere on output of command.\n%s", $text, implode("\n", $this->output))); + throw new \Exception(\sprintf("The text '%s' was found somewhere on output of command.\n%s", $text, implode("\n", $this->output))); } } } @@ -152,7 +152,7 @@ public function outputShouldBe(PyStringNode $string): void $expected = $string->getStrings(); foreach ($this->output as $index => $line) { if ($line !== $expected[$index]) { - throw new \Exception(sprintf("instead of\n%s", implode("\n", $this->output))); + throw new \Exception(\sprintf("instead of\n%s", implode("\n", $this->output))); } } } diff --git a/src/Json/JsonSchema.php b/src/Json/JsonSchema.php index 095b922b..246dac6c 100644 --- a/src/Json/JsonSchema.php +++ b/src/Json/JsonSchema.php @@ -34,7 +34,7 @@ public function validate(Json $json, Validator $validator) if (!$validator->isValid()) { $msg = 'JSON does not validate. Violations:'.\PHP_EOL; foreach ($validator->getErrors() as $error) { - $msg .= sprintf(' - [%s] %s'.\PHP_EOL, $error['property'], $error['message']); + $msg .= \sprintf(' - [%s] %s'.\PHP_EOL, $error['property'], $error['message']); } throw new \Exception($msg); }