Skip to content

Commit 2c104f1

Browse files
authored
Fix "False positive staticMethod.alreadyNarrowedType for Strings::length() inside numeric comparison" (#210)
1 parent 038d6e7 commit 2c104f1

6 files changed

Lines changed: 98 additions & 1 deletion

File tree

.github/workflows/e2e-tests.yml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# https://help.github.com/en/categories/automating-your-workflow-with-github-actions
2+
3+
name: "E2E Tests"
4+
5+
on:
6+
pull_request:
7+
push:
8+
branches:
9+
- "2.0.x"
10+
11+
concurrency:
12+
group: e2e-${{ github.head_ref || github.run_id }} # will be canceled on subsequent pushes in pull requests but not branches
13+
cancel-in-progress: true
14+
15+
permissions:
16+
contents: read
17+
18+
jobs:
19+
e2e-tests:
20+
name: "E2E tests"
21+
runs-on: "ubuntu-latest"
22+
timeout-minutes: 60
23+
24+
strategy:
25+
fail-fast: false
26+
matrix:
27+
include:
28+
- script: |
29+
cd e2e/bug-208
30+
composer install
31+
vendor/bin/phpstan
32+
33+
steps:
34+
- name: Harden the runner (Audit all outbound calls)
35+
uses: step-security/harden-runner@9af89fc71515a100421586dfdb3dc9c984fbf411 # v2.19.4
36+
with:
37+
egress-policy: audit
38+
39+
- name: "Checkout"
40+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
41+
42+
- name: "Install PHP"
43+
uses: "shivammathur/setup-php@7c071dfe9dc99bdf297fa79cb49ea005b9fcadbc" # v2.37.1
44+
with:
45+
coverage: "none"
46+
php-version: "8.3"
47+
48+
- uses: "ramsey/composer-install@65e4f84970763564f46a70b8a54b90d033b3bdda" # v4.0.0
49+
50+
- name: "Install bashunit"
51+
uses: "TypedDevs/bashunit@ffa9c79e71ecbb9990e777348bc9ba12314b62d0" # 0.39.1
52+
with:
53+
directory: "e2e"
54+
55+
- name: "Test"
56+
run: ${{ matrix.script }}

e2e/bug-208/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor
2+
composer.lock

e2e/bug-208/composer.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"require-dev": {
3+
"nette/utils": "^3.2.5",
4+
"phpstan/phpstan-nette": "@dev",
5+
"phpstan/phpstan": "@dev"
6+
},
7+
"repositories": [
8+
{
9+
"type": "path",
10+
"url": "../../"
11+
}
12+
]
13+
}

e2e/bug-208/phpstan.neon

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
includes:
2+
- ../../extension.neon
3+
- ../../rules.neon
4+
5+
parameters:
6+
level: 8
7+
paths:
8+
- src

e2e/bug-208/src/foo.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
use Nette\Utils\Strings;
5+
6+
function withoutPriorCheck(string $body): void
7+
{
8+
if (Strings::length($body) < 100) { // no error
9+
echo 'a';
10+
}
11+
}
12+
13+
function withPriorCheck(string $body): void
14+
{
15+
if ($body !== '' && Strings::length($body) < 100) { // false positive
16+
echo 'a';
17+
}
18+
}

src/Type/Nette/StringsLengthTypeSpecifiyingExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function specifyTypes(MethodReflection $staticMethodReflection, StaticCal
5454
new IntersectionType([new StringType(), new AccessoryNonEmptyStringType()]),
5555
$context,
5656
$scope,
57-
);
57+
)->setRootExpr($node);
5858
}
5959

6060
}

0 commit comments

Comments
 (0)