Skip to content

Commit

Permalink
Merge pull request #80 from sasezaki/coverage0210
Browse files Browse the repository at this point in the history
Refactor around test case
  • Loading branch information
sasezaki committed Feb 10, 2024
2 parents ab70ce5 + b399c7a commit 1e34e00
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/Rules/ContextKeyRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ private function originalPatternMatches(array $constantArrays, string $methodNam
$key = $keyType->getValue();

if (! is_string($key)) {
continue;
// keyType be string is checked by keysAreNonEmptyString()
// @codeCoverageIgnoreStart
continue; // @codeCoverageIgnoreEnd
}

$matched = preg_match($this->contextKeyOriginalPattern, $key, $matches);
Expand Down
16 changes: 6 additions & 10 deletions src/Rules/PlaceholderCorrespondToKeysRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ public function processNode(Node $node, Scope $scope): array

$context = $args[$contextArgumentNo];

$doesNohHaveError = self::contextDoesNotHavePlaceholderKey($scope->getType($context->value), $methodName, $matches[0], $matches[1]);
if ($doesNohHaveError instanceof RuleError) {
$errors[] = $doesNohHaveError;
$contextDoesNotHaveError = self::contextDoesNotHavePlaceholderKey($scope->getType($context->value), $methodName, $matches[0], $matches[1]);
if ($contextDoesNotHaveError instanceof RuleError) {
$errors[] = $contextDoesNotHaveError;
}
}

Expand All @@ -121,18 +121,14 @@ private static function contextDoesNotHavePlaceholderKey(Type $arrayType, string

$constantArrays = $arrayType->getConstantArrays();

if (count($constantArrays) === 0) {
return RuleErrorBuilder::message(
self::ERROR_EMPTY_CONTEXT
)->identifier('sfp-psr-log.placeholderCorrespondToKeysMissedKey')->build();
}

foreach ($constantArrays as $constantArray) {
$contextKeys = [];
$checkBraces = $braces;
foreach ($constantArray->getKeyTypes() as $keyType) {
if ($keyType->isString()->no()) {
continue;
// keyType be string checked by ContextKeyRule
// @codeCoverageIgnoreStart
continue; // @codeCoverageIgnoreEnd
}

$contextKeys[] = $keyType->getValue();
Expand Down
11 changes: 11 additions & 0 deletions test/Rules/ContextKeyRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ public function testAlwaysShouldBeCheckedNonEmptyString(): void
]);
}

public function testOriginalPattern(): void
{
$this->contextKeyOriginalPattern = '#\A[A-Za-z0-9-]+\z#';
$this->analyse([__DIR__ . '/data/contextKey_originalPattern.php'], [
[
'Parameter $context of logger method Psr\Log\LoggerInterface::info(), key should be match #\A[A-Za-z0-9-]+\z#.',
14,
],
]);
}

public function testEmptyStringPattern(): void
{
$this->contextKeyOriginalPattern = '';
Expand Down

0 comments on commit 1e34e00

Please sign in to comment.