Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Type/BitwiseFlagHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public function bitwiseOrContainsConstant(Expr $expr, Scope $scope, string $cons
}

if ($expr instanceof BitwiseOr) {
return TrinaryLogic::createFromBoolean($this->bitwiseOrContainsConstant($expr->left, $scope, $constName)->yes() ||
$this->bitwiseOrContainsConstant($expr->right, $scope, $constName)->yes());
return $this->bitwiseOrContainsConstant($expr->left, $scope, $constName)
->or($this->bitwiseOrContainsConstant($expr->right, $scope, $constName));
}

$fqcn = new FullyQualified($constName);
Expand Down
2 changes: 2 additions & 0 deletions tests/PHPStan/Analyser/nsrt/preg_split.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public function doWithVariables(string $pattern, string $subject, int $offset, i
assertType('list<array{string, int<0, max>}>|false', preg_split($pattern, $subject, $offset, PREG_SPLIT_OFFSET_CAPTURE));
assertType("list<string>|false", preg_split($pattern, $subject, $offset, PREG_SPLIT_DELIM_CAPTURE));
assertType('list<array{string, int<0, max>}>|false', preg_split($pattern, $subject, $offset, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE));
assertType('list<array{string, int<0, max>}|string>|false', preg_split($pattern, $subject, $offset, PREG_SPLIT_DELIM_CAPTURE | $flags));
assertType('list<array{string, int<0, max>}|string>|false', preg_split($pattern, $subject, $offset, $flags | PREG_SPLIT_DELIM_CAPTURE));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,16 @@ public function testBug4814(): void
]);
}

public function testJsonMaybeThrowWithDynamicFlags(): void
{
$this->analyse([__DIR__ . '/data/json-maybe-throw-dynamic-flags.php'], [
[
'Dead catch - JsonException is never thrown in the try block.',
35,
],
]);
}

public function testBug9066(): void
{
$this->analyse([__DIR__ . '/data/bug-9066.php'], [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace JsonMaybeThrowDynamicFlags;

function (string $s, int $flags): void {
try {
json_decode($s, true, 512, JSON_BIGINT_AS_STRING | $flags);
} catch (\JsonException $e) {

}
};

function (string $s, int $flags): void {
try {
json_decode($s, true, 512, $flags | JSON_BIGINT_AS_STRING);
} catch (\JsonException $e) {

}
};

/**
* @param mixed $m
*/
function ($m, int $flags): void {
try {
json_encode($m, JSON_PRETTY_PRINT | $flags);
} catch (\JsonException $e) {

}
};

function (string $s): void {
try {
json_decode($s, true, 512, JSON_BIGINT_AS_STRING | JSON_INVALID_UTF8_IGNORE);
} catch (\JsonException $e) {

}
};
24 changes: 24 additions & 0 deletions tests/PHPStan/Type/BitwiseFlagHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,30 @@ public static function dataJsonExprContainsConst(): array
'JSON_THROW_ON_ERROR',
TrinaryLogic::createNo(),
],
[
new BitwiseOr(
new ConstFetch(new FullyQualified('JSON_NUMERIC_CHECK')),
new Variable('integerVar'),
),
'JSON_THROW_ON_ERROR',
TrinaryLogic::createMaybe(),
],
[
new BitwiseOr(
new Variable('integerVar'),
new ConstFetch(new FullyQualified('JSON_THROW_ON_ERROR')),
),
'JSON_THROW_ON_ERROR',
TrinaryLogic::createYes(),
],
[
new BitwiseOr(
new ConstFetch(new FullyQualified('JSON_NUMERIC_CHECK')),
new Variable('stringVar'),
),
'JSON_THROW_ON_ERROR',
TrinaryLogic::createNo(),
],
[
new Variable('mixedVar'),
'JSON_THROW_ON_ERROR',
Expand Down
Loading