Skip to content
This repository was archived by the owner on Sep 1, 2024. It is now read-only.

Commit c03c85f

Browse files
committed
Align repo with latest cases
1 parent 258aba7 commit c03c85f

File tree

6 files changed

+20
-39
lines changed

6 files changed

+20
-39
lines changed

README.md

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,15 @@
11
# phpstan-require-file-exists
2-
Weirdly enough, PHPStan does not check if a file exists when used in a
2+
**UPDATE:** This rule has been merged into PHPStan itself. Here is the PR: https://github.com/phpstan/phpstan-src/pull/3294
3+
4+
~~Weirdly enough, PHPStan does not check if a file exists when used in a
35
`require` or `include` statement. This is a PHPStan rule that tries to do
4-
exactly that.
6+
exactly that.~~
57

68
## Installation
79
```bash
810
composer require --dev bellangelo/phpstan-require-file-exists
911
```
1012

11-
## Configuration
12-
Add the following to your `phpstan.neon`:
13-
```neon
14-
services:
15-
-
16-
class: Bellangelo\PHPStanRequireFileExists\RequireFileExistsRule
17-
arguments:
18-
- @reflectionProvider
19-
tags: [phpstan.rules.rule]
20-
```
21-
22-
You can find a `phpstan.neon` example in the `tests` directory, here: [tests/phpstan.neon](tests/phpstan-testing.neon).
23-
24-
## Supported cases
25-
- `require 'file.php';` - might not find it since the path is relative.
26-
- `require __DIR__ . '/file.php';` - can find it, if it exists.
27-
- `require __DIR__ . '/' . MyClass::MY_CONST;` - can find it if the const has the correct value from the start.
28-
- `require __DIR__ . '/' . MY_CONST;` - can find it if the constant is defined in the bootstrap file.
29-
30-
## Unsupported cases
31-
- `require $file;` - won't throw an error since it cannot read variables.
32-
- `require (new MyClass())->file;` - won't throw an error since it cannot read class properties.
33-
- `require (new MyClass())->getFile();` - won't throw an error since it cannot read class methods.
34-
- `require getFile();` - won't throw an error since it cannot read functions.
35-
3613
## Development
3714
For local development and testing, `composer.json` contains several commands that you can run.
3815
- `composer run tests ` - runs the PHPUnit tests.

src/RequireFileExistsRule.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
use PHPStan\Rules\Rule;
1313
use PHPStan\Rules\RuleErrorBuilder;
1414
use PHPStan\ShouldNotHappenException;
15+
1516
use function array_merge;
1617
use function dirname;
1718
use function explode;
1819
use function get_include_path;
1920
use function is_file;
2021
use function sprintf;
22+
2123
use const PATH_SEPARATOR;
2224

2325
/**
@@ -57,8 +59,8 @@ public function processNode(Node $node, Scope $scope): array
5759
* We cannot use `stream_resolve_include_path` as it works based on the calling script.
5860
* This method simulates the behavior of `stream_resolve_include_path` but for the given scope.
5961
* The priority order is the following:
60-
* 1. The current working directory.
61-
* 2. The include path.
62+
* 1. The current working directory.
63+
* 2. The include path.
6264
* 3. The path of the script that is being executed.
6365
*/
6466
private function doesFileExist(string $path, Scope $scope): bool
@@ -80,6 +82,7 @@ private function doesFileExist(string $path, Scope $scope): bool
8082

8183
private function doesFileExistForDirectory(string $path, string $workingDirectory): bool
8284
{
85+
/** @phpstan-ignore-next-line */
8386
$fileHelper = new FileHelper($workingDirectory);
8487
$normalisedPath = $fileHelper->normalizePath($path);
8588
$absolutePath = $fileHelper->absolutizePath($normalisedPath);
@@ -138,5 +141,4 @@ private function resolveFilePaths(Include_ $node, Scope $scope): array
138141

139142
return $paths;
140143
}
141-
142-
}
144+
}

tests/RequireFileExistsRuleTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
<?php
22

3-
declare(strict_types = 1);
3+
declare(strict_types=1);
44

55
namespace Tests;
66

77
use Bellangelo\PHPStanRequireFileExists\RequireFileExistsRule;
88
use PHPStan\Rules\Rule;
99
use PHPStan\Testing\RuleTestCase;
10+
1011
use function get_include_path;
1112
use function implode;
1213
use function realpath;
1314
use function set_include_path;
15+
1416
use const PATH_SEPARATOR;
1517

1618
/**
1719
* @extends RuleTestCase<RequireFileExistsRule>
1820
*/
1921
class RequireFileExistsRuleTest extends RuleTestCase
2022
{
21-
2223
private RequireFileExistsRule $rule;
2324

2425
public function setUp(): void
@@ -135,5 +136,4 @@ public function testRelativePathWithSameWorkingDirectory(): void
135136
$this->rule = $this->getDefaultRule();
136137
}
137138
}
138-
139-
}
139+
}

tests/data/require-file-conditionally.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
$path = __DIR__ . '/include-me-to-prove-you-work.txt';
66

7-
if (rand(0,1)) {
7+
if (rand(0, 1)) {
88
$path = 'a-file-that-does-not-exist.php';
99
}
1010

tests/data/require-file-relative-path.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010
include 'data/include-me-to-prove-you-work.txt';
1111
include_once 'data/include-me-to-prove-you-work.txt';
1212
require 'data/include-me-to-prove-you-work.txt';
13-
require_once 'data/include-me-to-prove-you-work.txt';
13+
require_once 'data/include-me-to-prove-you-work.txt';

tests/data/require-file-simple-case.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php declare(strict_types=1);
1+
<?php
2+
3+
declare(strict_types=1);
24

35
$fileThatExists = __DIR__ . '/include-me-to-prove-you-work.txt';
46
$fileThatDoesNotExist = 'a-file-that-does-not-exist.php';
@@ -11,4 +13,4 @@
1113
include $fileThatDoesNotExist;
1214
include_once $fileThatDoesNotExist;
1315
require $fileThatDoesNotExist;
14-
require_once $fileThatDoesNotExist;
16+
require_once $fileThatDoesNotExist;

0 commit comments

Comments
 (0)