Skip to content

Commit

Permalink
Merge pull request #83 from sasezaki/identifer
Browse files Browse the repository at this point in the history
PHPStan 1.11 support - error identifier must not includes hyphen
  • Loading branch information
sasezaki committed May 13, 2024
2 parents 1e34e00 + 8996c26 commit 5581dd1
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 24 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ This package provides the following rules.
| :pushpin: _error identifier_ |
| --- |
| sfp-psr-log.placeholderCharactersInvalidChar |
| sfpPsrLog.placeholderCharactersInvalidChar |

* reports when placeholder in `$message` characters are **not**, `A-Z`, `a-z`, `0-9`, underscore `_`, and period `.`

Expand All @@ -57,7 +57,7 @@ $logger->info('message are {foo-hyphen}');

| :pushpin: _error identifier_ |
| --- |
| sfp-psr-log.placeholderCharactersDoubleBraces |
| sfpPsrLog.placeholderCharactersDoubleBraces |

* reports when double braces pair `{{` `}}` are used.

Expand All @@ -72,7 +72,7 @@ $logger->info('message are {{foo}}');
| :pushpin: _error identifier_ |
| --- |
| sfp-psr-log.placeholderCorrespondToKeysMissedContext |
| sfpPsrLog.placeholderCorrespondToKeysMissedContext |

* reports when placeholder exists in message, but `$context` parameter is missed.

Expand All @@ -83,7 +83,7 @@ $logger->info('message has {nonContext} .');

| :pushpin: _error identifier_ |
| --- |
| sfp-psr-log.placeholderCorrespondToKeysMissedKey |
| sfpPsrLog.placeholderCorrespondToKeysMissedKey |

* reports when placeholder exists in message, but key in `$context` does not exist against them.

Expand All @@ -99,7 +99,7 @@ $logger->info('user {user_id} gets an error {error} .', ['user_id' => $user_id])
| :pushpin: _error identifier_ |
| --- |
| sfp-psr-log.contextKeyNonEmptyString |
| sfpPsrLog.contextKeyNonEmptyString |

* reports when context key is not **non-empty-string**.

Expand All @@ -110,7 +110,7 @@ $logger->info('user {user_id} gets an error {error} .', ['user_id' => $user_id])

| :pushpin: _error identifier_ |
| --- |
| sfp-psr-log.contextKeyOriginalPattern |
| sfpPsrLog.contextKeyOriginalPattern |

* reports when context key is not matched you defined pattern.
* if `contextKeyOriginalPattern` parameter is not set, this check would be ignored.

Check failure on line 116 in README.md

View workflow job for this annotation

GitHub Actions / ci / QA Checks (README Linting [7.2, latest], ubuntu-latest, laminas/laminas-continuous-integration-ac...

Unordered list indentation [Expected: 4; Actual: 2]
Expand All @@ -132,7 +132,7 @@ parameters:
| :pushpin: _error identifier_ |
| --- |
| sfp-psr-log.contextRequireExceptionKey |
| sfpPsrLog.contextRequireExceptionKey |

* It forces `exception` key into context parameter when current scope has `\Throwable` object.

Expand Down Expand Up @@ -194,7 +194,7 @@ parameters:

| :pushpin: _error identifier_ |
| --- |
| sfp-psr-log.messageNotStaticString |
| sfpPsrLog.messageNotStaticString |

* reports when $message is not static string value.

Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
],
"require": {
"php": "^7.2.0 || ~8.0.0 || ~8.1.0 || ~8.2.0 || ~8.3.0",
"phpstan/phpstan": "^1.10",
"phpstan/phpstan": "^1.11",
"struggle-for-php/sfp-stubs-psr-log": "^1.0.1 || ^2 || ^3.0.1"
},
"require-dev": {
Expand All @@ -21,6 +21,9 @@
"squizlabs/php_codesniffer": "^3.7",
"vimeo/psalm": "^4 || ^5.9"
},
"conflict": {
"nikic/php-parser": "<4.13.0"
},
"autoload": {
"psr-4": {
"Sfp\\PHPStan\\Psr\\Log\\": "src"
Expand Down
8 changes: 4 additions & 4 deletions src/Rules/ContextKeyRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function processNode(Node $node, Scope $scope): array
* phpcs:ignore SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly
* @phpstan-param list<\PHPStan\Type\Constant\ConstantArrayType> $constantArrays
* phpcs:ignore SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly
* @phpstan-return list<\PHPStan\Rules\RuleError>
* @phpstan-return list<\PHPStan\Rules\IdentifierRuleError>
*/
private static function keysAreNonEmptyString(array $constantArrays, string $methodName): array
{
Expand All @@ -123,7 +123,7 @@ private static function keysAreNonEmptyString(array $constantArrays, string $met

$errors[] = RuleErrorBuilder::message(
sprintf(self::ERROR_NOT_NON_EMPTY_STRING, $methodName)
)->identifier('sfp-psr-log.contextKeyNonEmptyString')->build();
)->identifier('sfpPsrLog.contextKeyNonEmptyString')->build();
}
}

Expand All @@ -134,7 +134,7 @@ private static function keysAreNonEmptyString(array $constantArrays, string $met
* phpcs:ignore SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly
* @phpstan-param list<\PHPStan\Type\Constant\ConstantArrayType> $constantArrays
* phpcs:ignore SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly
* @phpstan-return list<\PHPStan\Rules\RuleError>
* @phpstan-return list<\PHPStan\Rules\IdentifierRuleError>
*/
private function originalPatternMatches(array $constantArrays, string $methodName): array
{
Expand Down Expand Up @@ -166,7 +166,7 @@ private function originalPatternMatches(array $constantArrays, string $methodNam
if ($matched === 0) {
$errors[] = RuleErrorBuilder::message(
sprintf(self::ERROR_NOT_MATCH_ORIGINAL_PATTERN, $methodName, $this->contextKeyOriginalPattern)
)->identifier('sfp-psr-log.contextKeyOriginalPattern')->build();
)->identifier('sfpPsrLog.contextKeyOriginalPattern')->build();
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/Rules/ContextRequireExceptionKeyRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

return [sprintf(self::ERROR_MISSED_EXCEPTION_KEY, $methodName, "\${$throwable}")];
return [
RuleErrorBuilder::message(
sprintf(self::ERROR_MISSED_EXCEPTION_KEY, $methodName, "\${$throwable}")
)->identifier('sfpPsrLog.contextRequireExceptionKey')->build(),
];
}

$context = $args[$contextArgumentNo];
Expand All @@ -121,7 +125,7 @@ public function processNode(Node $node, Scope $scope): array
return [
RuleErrorBuilder::message(
sprintf(self::ERROR_MISSED_EXCEPTION_KEY, $methodName, "\${$throwable}")
)->identifier('sfp-psr-log.contextRequireExceptionKey')->build(),
)->identifier('sfpPsrLog.contextRequireExceptionKey')->build(),
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Rules/MessageStaticStringRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function processNode(Node $node, Scope $scope): array
RuleErrorBuilder::message(
sprintf(self::ERROR_MESSAGE_NOT_STATIC, $methodName)
)
->identifier('sfp-psr-log.messageNotStaticString')
->identifier('sfpPsrLog.messageNotStaticString')
->tip('See https://www.php-fig.org/psr/psr-3/meta/#static-log-messages')
->build(),
];
Expand Down
9 changes: 5 additions & 4 deletions src/Rules/PlaceholderCharactersRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
Expand Down Expand Up @@ -100,7 +101,7 @@ public function processNode(Node $node, Scope $scope): array
return $errors;
}

private static function checkDoubleBrace(string $message, string $methodName): ?RuleError
private static function checkDoubleBrace(string $message, string $methodName): ?IdentifierRuleError
{
$matched = preg_match_all('#{{(.+?)}}#', $message, $matches);

Expand All @@ -111,12 +112,12 @@ private static function checkDoubleBrace(string $message, string $methodName): ?
return RuleErrorBuilder::message(
sprintf(self::ERROR_DOUBLE_BRACES, $methodName, implode(',', $matches[0]))
)
->identifier('sfp-psr-log.placeholderCharactersDoubleBraces')
->identifier('sfpPsrLog.placeholderCharactersDoubleBraces')
->tip('See https://www.php-fig.org/psr/psr-3/#12-message')
->build();
}

private static function checkInvalidChar(string $message, string $methodName): ?RuleError
private static function checkInvalidChar(string $message, string $methodName): ?IdentifierRuleError
{
$matched = preg_match_all('#{(.+?)}#', $message, $matches);

Expand All @@ -138,7 +139,7 @@ private static function checkInvalidChar(string $message, string $methodName): ?
return RuleErrorBuilder::message(
sprintf(self::ERROR_INVALID_CHAR, $methodName, implode(',', $invalidPlaceHolders))
)
->identifier('sfp-psr-log.placeholderCharactersInvalidChar')
->identifier('sfpPsrLog.placeholderCharactersInvalidChar')
->tip('See https://www.php-fig.org/psr/psr-3/#12-message')
->build();
}
Expand Down
9 changes: 5 additions & 4 deletions src/Rules/PlaceholderCorrespondToKeysRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
Expand Down Expand Up @@ -91,7 +92,7 @@ public function processNode(Node $node, Scope $scope): array
if (! isset($args[$contextArgumentNo])) {
$errors[] = RuleErrorBuilder::message(
sprintf(self::ERROR_MISSED_CONTEXT, $methodName, implode(',', $matches[0]))
)->identifier('sfp-psr-log.placeholderCorrespondToKeysMissedContext')->build();
)->identifier('sfpPsrLog.placeholderCorrespondToKeysMissedContext')->build();

continue;
}
Expand All @@ -111,12 +112,12 @@ public function processNode(Node $node, Scope $scope): array
* @phpstan-param list<string> $braces
* @phpstan-param list<string> $placeholders
*/
private static function contextDoesNotHavePlaceholderKey(Type $arrayType, string $methodName, array $braces, array $placeholders): ?RuleError
private static function contextDoesNotHavePlaceholderKey(Type $arrayType, string $methodName, array $braces, array $placeholders): ?IdentifierRuleError
{
if ($arrayType->isIterableAtLeastOnce()->no()) {
return RuleErrorBuilder::message(
self::ERROR_EMPTY_CONTEXT
)->identifier('sfp-psr-log.placeholderCorrespondToKeysMissedKey')->build();
)->identifier('sfpPsrLog.placeholderCorrespondToKeysMissedKey')->build();
}

$constantArrays = $arrayType->getConstantArrays();
Expand Down Expand Up @@ -146,7 +147,7 @@ private static function contextDoesNotHavePlaceholderKey(Type $arrayType, string

return RuleErrorBuilder::message(
sprintf(self::ERROR_MISSED_KEY, $methodName, implode(',', $checkBraces))
)->identifier('sfp-psr-log.placeholderCorrespondToKeysMissedKey')->build();
)->identifier('sfpPsrLog.placeholderCorrespondToKeysMissedKey')->build();
}

return null;
Expand Down

0 comments on commit 5581dd1

Please sign in to comment.