Skip to content

Commit

Permalink
Merge pull request #35 from Jean-Beru/feat/optimize-sprintf
Browse files Browse the repository at this point in the history
feat: use optimized sprintf
  • Loading branch information
Jean-Beru authored Aug 8, 2024
2 parents 509aeef + 91ef29c commit 1ad1756
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/Asserter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Context/DebugContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
18 changes: 9 additions & 9 deletions src/Context/JsonContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand All @@ -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);
}
}

Expand All @@ -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));
}
}

Expand All @@ -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)));
}
}

Expand All @@ -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));
}
}

Expand All @@ -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)));
}
}

Expand All @@ -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)));
}
}

Expand All @@ -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));
}
}

Expand All @@ -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));
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Context/RestContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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))
);
}

Expand Down
14 changes: 7 additions & 7 deletions src/Context/SystemContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand All @@ -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));
}
}

Expand All @@ -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));
}
}

Expand All @@ -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));
}
}

Expand All @@ -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)));
}
}

Expand All @@ -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)));
}
}
}
Expand All @@ -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)));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Json/JsonSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 1ad1756

Please # to comment.