Skip to content

Commit 1e34e00

Browse files
authored
Merge pull request #80 from sasezaki/coverage0210
Refactor around test case
2 parents ab70ce5 + b399c7a commit 1e34e00

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

src/Rules/ContextKeyRule.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ private function originalPatternMatches(array $constantArrays, string $methodNam
152152
$key = $keyType->getValue();
153153

154154
if (! is_string($key)) {
155-
continue;
155+
// keyType be string is checked by keysAreNonEmptyString()
156+
// @codeCoverageIgnoreStart
157+
continue; // @codeCoverageIgnoreEnd
156158
}
157159

158160
$matched = preg_match($this->contextKeyOriginalPattern, $key, $matches);

src/Rules/PlaceholderCorrespondToKeysRule.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ public function processNode(Node $node, Scope $scope): array
9898

9999
$context = $args[$contextArgumentNo];
100100

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

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

122122
$constantArrays = $arrayType->getConstantArrays();
123123

124-
if (count($constantArrays) === 0) {
125-
return RuleErrorBuilder::message(
126-
self::ERROR_EMPTY_CONTEXT
127-
)->identifier('sfp-psr-log.placeholderCorrespondToKeysMissedKey')->build();
128-
}
129-
130124
foreach ($constantArrays as $constantArray) {
131125
$contextKeys = [];
132126
$checkBraces = $braces;
133127
foreach ($constantArray->getKeyTypes() as $keyType) {
134128
if ($keyType->isString()->no()) {
135-
continue;
129+
// keyType be string checked by ContextKeyRule
130+
// @codeCoverageIgnoreStart
131+
continue; // @codeCoverageIgnoreEnd
136132
}
137133

138134
$contextKeys[] = $keyType->getValue();

test/Rules/ContextKeyRuleTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,17 @@ public function testAlwaysShouldBeCheckedNonEmptyString(): void
5858
]);
5959
}
6060

61+
public function testOriginalPattern(): void
62+
{
63+
$this->contextKeyOriginalPattern = '#\A[A-Za-z0-9-]+\z#';
64+
$this->analyse([__DIR__ . '/data/contextKey_originalPattern.php'], [
65+
[
66+
'Parameter $context of logger method Psr\Log\LoggerInterface::info(), key should be match #\A[A-Za-z0-9-]+\z#.',
67+
14,
68+
],
69+
]);
70+
}
71+
6172
public function testEmptyStringPattern(): void
6273
{
6374
$this->contextKeyOriginalPattern = '';

0 commit comments

Comments
 (0)