Skip to content

Commit

Permalink
Merge pull request #74 from sasezaki/bleedingEdge
Browse files Browse the repository at this point in the history
Enable phpstan bleedingEdge
  • Loading branch information
sasezaki committed Feb 3, 2024
2 parents 62dfeb6 + eb5777d commit 9ea7d4d
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 35 deletions.
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ parameters:
- test/Rules/data/*

includes:
- phar://phpstan.phar/conf/bleedingEdge.neon
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-phpunit/rules.neon
41 changes: 19 additions & 22 deletions src/Rules/ContextKeyRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\ObjectType;

use function count;
use function in_array;
use function is_string;
use function preg_match;
use function sprintf;

Expand Down Expand Up @@ -57,6 +57,7 @@ public function processNode(Node $node, Scope $scope): array
return []; // @codeCoverageIgnoreEnd
}

/** @var Node\Arg[] $args */
$args = $node->getArgs();
if (count($args) === 0) {
// @codeCoverageIgnoreStart
Expand All @@ -65,25 +66,24 @@ public function processNode(Node $node, Scope $scope): array

$methodName = $node->name->toLowerString();

if ($methodName !== 'log' && ! in_array($methodName, LogLevelListInterface::LOGGER_LEVEL_METHODS)) {
// @codeCoverageIgnoreStart
return []; // @codeCoverageIgnoreEnd
}

$contextArgumentNo = 1;
if ($methodName === 'log') {
if (count($args) < 2) {
// @codeCoverageIgnoreStart
return []; // @codeCoverageIgnoreEnd
if (count($args) < 3) {
return [];
}

$contextArgumentNo = 2;
} elseif (! in_array($methodName, LogLevelListInterface::LOGGER_LEVEL_METHODS)) {
// @codeCoverageIgnoreStart
return []; // @codeCoverageIgnoreEnd
} elseif (count($args) < 2) {
return [];
}

$context = $args[$contextArgumentNo];

if (! $context instanceof Node\Arg) {
return [];
}

$arrayType = $scope->getType($context->value);

if ($arrayType->isIterableAtLeastOnce()->no()) {
Expand Down Expand Up @@ -117,18 +117,13 @@ private static function keysAreNonEmptyString(array $constantArrays, string $met
$errors = [];
foreach ($constantArrays as $constantArray) {
foreach ($constantArray->getKeyTypes() as $keyType) {
if (! $keyType instanceof ConstantStringType) {
$errors[] = RuleErrorBuilder::message(
sprintf(self::ERROR_NOT_NON_EMPTY_STRING, $methodName)
)->identifier('sfp-psr-log.contextKeyNonEmptyString')->build();
if ($keyType->isNonEmptyString()->yes()) {
continue;
}

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

Expand All @@ -154,11 +149,13 @@ private function originalPatternMatches(array $constantArrays, string $methodNam
$errors = [];
foreach ($constantArrays as $constantArray) {
foreach ($constantArray->getKeyTypes() as $keyType) {
if (! $keyType instanceof ConstantStringType) {
$key = $keyType->getValue();

if (! is_string($key)) {
continue;
}

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

if ($matched === false) {
throw new LogicException(sprintf('provided regex pattern %s is invalid', $this->contextKeyOriginalPattern));
Expand Down
7 changes: 2 additions & 5 deletions src/Rules/ContextRequireExceptionKeyRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\ObjectType;
use Throwable;

use function assert;
use function count;
use function in_array;
use function sprintf;
Expand Down Expand Up @@ -66,6 +64,7 @@ public function processNode(Node $node, Scope $scope): array
return []; // @codeCoverageIgnoreEnd
}

/** @var Node\Arg[] $args */
$args = $node->getArgs();
if (count($args) === 0) {
// @codeCoverageIgnoreStart
Expand Down Expand Up @@ -113,8 +112,6 @@ public function processNode(Node $node, Scope $scope): array
}

$context = $args[$contextArgumentNo];
/** @psalm-suppress RedundantConditionGivenDocblockType */
assert($context instanceof Node\Arg);

if (self::contextDoesNotHaveExceptionKey($context, $scope)) {
if (! $this->hasReportLogLevel($logLevels)) {
Expand Down Expand Up @@ -164,7 +161,7 @@ private function findCurrentScopeThrowableVariable(Scope $scope): ?string
private static function contextDoesNotHaveExceptionKey(Node\Arg $context, Scope $scope): bool
{
$type = $scope->getType($context->value);
if ($type instanceof ArrayType) {
if ($type->isArray()->yes()) {
if ($type->hasOffsetValueType(new ConstantStringType('exception'))->yes()) {
return false;
}
Expand Down
4 changes: 1 addition & 3 deletions src/Rules/MessageStaticStringRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\ObjectType;

use function assert;
use function count;
use function in_array;
use function sprintf;
Expand Down Expand Up @@ -44,6 +43,7 @@ public function processNode(Node $node, Scope $scope): array
return []; // @codeCoverageIgnoreEnd
}

/** @var Node\Arg[] $args */
$args = $node->getArgs();
if (count($args) === 0) {
// @codeCoverageIgnoreStart
Expand All @@ -69,8 +69,6 @@ public function processNode(Node $node, Scope $scope): array
}

$message = $args[$messageArgumentNo];
/** @psalm-suppress RedundantConditionGivenDocblockType */
assert($message instanceof Node\Arg);
$value = $scope->getType($message->value);
$strings = $value->getConstantStrings();

Expand Down
4 changes: 1 addition & 3 deletions src/Rules/PlaceholderCharactersRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\ObjectType;

use function assert;
use function count;
use function implode;
use function in_array;
Expand Down Expand Up @@ -49,6 +48,7 @@ public function processNode(Node $node, Scope $scope): array
return []; // @codeCoverageIgnoreEnd
}

/** @var Node\Arg[] $args */
$args = $node->getArgs();
if (count($args) === 0) {
// @codeCoverageIgnoreStart
Expand All @@ -74,8 +74,6 @@ public function processNode(Node $node, Scope $scope): array
}

$message = $args[$messageArgumentNo];
/** @psalm-suppress RedundantConditionGivenDocblockType */
assert($message instanceof Node\Arg);

$strings = $scope->getType($message->value)->getConstantStrings();

Expand Down
4 changes: 2 additions & 2 deletions src/Rules/PlaceholderCorrespondToKeysRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;

Expand Down Expand Up @@ -132,9 +131,10 @@ private static function contextDoesNotHavePlaceholderKey(Type $arrayType, string
$contextKeys = [];
$checkBraces = $braces;
foreach ($constantArray->getKeyTypes() as $keyType) {
if (! $keyType instanceof ConstantStringType) {
if ($keyType->isString()->no()) {
continue;
}

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

Expand Down
3 changes: 3 additions & 0 deletions test/Rules/data/contextKey_nonEmptyString.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@ function main(Psr\Log\LoggerInterface $logger, array $nonTypedArray, array $cons
$ngArray = [1 => __LINE__];
$logger->info('ok', $okArray);
$logger->info('ng', $ngArray); //ng

// ok - without context - should be ignored
$logger->info('foo');
}

0 comments on commit 9ea7d4d

Please sign in to comment.