-
Notifications
You must be signed in to change notification settings - Fork 3
feat: Add playground for testing and improve editorUrl documentation #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,3 +38,7 @@ parameters: | |
| friendly: | ||
| lineBefore: 2 | ||
| lineAfter: 2 | ||
| # editorUrl examples: | ||
| # - PhpStorm: 'phpstorm://open?file=%%relFile%%&line=%%line%%' | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This example for PhpStorm uses |
||
| # - VSCode (local): 'vscode://file//path/to/project/%%relFile%%:%%line%%' | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The comment |
||
| editorUrl: null | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * Dead code and unreachable code errors for PHPStan testing | ||
| */ | ||
|
|
||
| // 1. Unreachable code after return | ||
| function unreachableAfterReturn(): string | ||
| { | ||
| return 'hello'; | ||
| echo 'This is unreachable'; // Error: unreachable | ||
| } | ||
|
|
||
| // 2. Unreachable code after throw | ||
| function unreachableAfterThrow(): void | ||
| { | ||
| throw new RuntimeException('Error'); | ||
| echo 'Never executed'; // Error: unreachable | ||
| } | ||
|
|
||
| // 3. Always true/false condition | ||
| function alwaysTrueCondition(): void | ||
| { | ||
| $value = 'string'; | ||
| if (is_string($value)) { // Error: always true | ||
| echo 'Always executes'; | ||
| } | ||
| } | ||
|
|
||
| // 4. Impossible type check | ||
| function impossibleCheck(int $value): void | ||
| { | ||
| if (is_string($value)) { // Error: impossible, $value is int | ||
| echo 'Never happens'; | ||
| } | ||
| } | ||
|
|
||
| // 5. Unused private method | ||
| class ClassWithDeadCode | ||
| { | ||
| public function publicMethod(): void | ||
| { | ||
| echo 'public'; | ||
| } | ||
|
|
||
| private function unusedPrivateMethod(): void // Error: unused | ||
| { | ||
| echo 'never called'; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * Logic and comparison errors for PHPStan testing | ||
| */ | ||
|
|
||
| // 1. Strict comparison with different types | ||
| function strictComparison(): bool | ||
| { | ||
| return 'string' === 123; // Error: comparing different types | ||
| } | ||
|
|
||
| // 2. Division by zero | ||
| function divisionByZero(): float | ||
| { | ||
| $divisor = 0; | ||
| return 100 / $divisor; // Error: division by zero | ||
| } | ||
|
|
||
| // 3. Array access on non-array | ||
| function arrayAccessOnString(): string | ||
| { | ||
| $value = 'hello'; | ||
| return $value['key']; // Error: invalid array access | ||
| } | ||
|
|
||
| // 4. Instanceof with final class that doesn't match | ||
| final class FinalClass | ||
| { | ||
| } | ||
|
|
||
| class OtherClass | ||
| { | ||
| } | ||
|
|
||
| function impossibleInstanceof(OtherClass $obj): bool | ||
| { | ||
| return $obj instanceof FinalClass; // Error: impossible | ||
| } | ||
|
|
||
| // 5. Wrong number of arguments | ||
| function expectsThreeArgs(int $a, int $b, int $c): int | ||
| { | ||
| return $a + $b + $c; | ||
| } | ||
|
|
||
| $result = expectsThreeArgs(1, 2); // Error: missing argument | ||
|
|
||
| // 6. Duplicate array key | ||
| function duplicateKeys(): array | ||
| { | ||
| return [ | ||
| 'key' => 'first', | ||
| 'key' => 'second', // Error: duplicate key | ||
| ]; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * Missing type declarations for PHPStan testing | ||
| */ | ||
|
|
||
| // 1. Missing return type | ||
| function noReturnType() // Error at higher levels: missing return type | ||
| { | ||
| return 'hello'; | ||
| } | ||
|
|
||
| // 2. Missing parameter type | ||
| function noParamType($value): string // Error at higher levels: missing param type | ||
| { | ||
| return (string) $value; | ||
| } | ||
|
|
||
| // 3. Missing property type | ||
| class MissingTypes | ||
| { | ||
| public $untyped; // Error at higher levels: missing property type | ||
|
|
||
| private $alsoUntyped = 'default'; // Error at higher levels | ||
|
|
||
| public function process($input) // Error: missing types | ||
| { | ||
| $this->untyped = $input; | ||
| return $input; | ||
| } | ||
| } | ||
|
|
||
| // 4. Mixed type usage | ||
| function usesMixed(mixed $value): mixed | ||
| { | ||
| return $value->someMethod(); // Error: calling method on mixed | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # PHPStan configuration for playground | ||
| # Usage: cd playground && ../vendor/bin/phpstan analyze | ||
|
|
||
| includes: | ||
| - ../extension.neon | ||
|
|
||
| parameters: | ||
| level: 9 | ||
| paths: | ||
| - . | ||
|
|
||
| # Friendly formatter settings | ||
| friendly: | ||
| lineBefore: 3 | ||
| lineAfter: 3 | ||
| editorUrl: 'vscode://file/%%relFile%%:%%line%%' | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * Type-related errors for PHPStan testing | ||
| */ | ||
|
|
||
| // 1. Wrong return type | ||
| function getString(): string | ||
| { | ||
| return 123; // Error: should return string | ||
| } | ||
|
|
||
| // 2. Wrong argument type | ||
| function expectsInt(int $value): void | ||
| { | ||
| echo $value; | ||
| } | ||
|
|
||
| expectsInt('not an int'); // Error: wrong argument type | ||
|
|
||
| // 3. Nullable type not handled | ||
| function processString(string $value): int | ||
| { | ||
| return strlen($value); | ||
| } | ||
|
|
||
| function mayReturnNull(): ?string | ||
| { | ||
| return rand(0, 1) ? 'hello' : null; | ||
| } | ||
|
|
||
| processString(mayReturnNull()); // Error: might be null | ||
|
|
||
| // 4. Array type mismatch | ||
| /** @param array<int, string> $items */ | ||
| function processStringArray(array $items): void | ||
| { | ||
| foreach ($items as $item) { | ||
| echo $item; | ||
| } | ||
| } | ||
|
|
||
| processStringArray([1, 2, 3]); // Error: array of int, not string | ||
|
|
||
| // 5. Property type mismatch | ||
| class TypedClass | ||
| { | ||
| public string $name; | ||
|
|
||
| public function __construct() | ||
| { | ||
| $this->name = 42; // Error: wrong type | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * Undefined variable/method/class errors for PHPStan testing | ||
| */ | ||
|
|
||
| // 1. Undefined variable | ||
| function useUndefinedVar(): void | ||
| { | ||
| echo $undefinedVariable; // Error: undefined variable | ||
| } | ||
|
|
||
| // 2. Undefined method | ||
| class SomeClass | ||
| { | ||
| public function existingMethod(): void | ||
| { | ||
| } | ||
| } | ||
|
|
||
| $obj = new SomeClass(); | ||
| $obj->nonExistentMethod(); // Error: undefined method | ||
|
|
||
| // 3. Undefined class | ||
| $instance = new NonExistentClass(); // Error: undefined class | ||
|
|
||
| // 4. Undefined constant | ||
| echo UNDEFINED_CONSTANT; // Error: undefined constant | ||
|
|
||
| // 5. Undefined property | ||
| class AnotherClass | ||
| { | ||
| public string $definedProperty = 'hello'; | ||
| } | ||
|
|
||
| $another = new AnotherClass(); | ||
| echo $another->undefinedProperty; // Error: undefined property | ||
|
|
||
| // 6. Undefined function | ||
| undefinedFunction(); // Error: undefined function |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The double slash
//aftervscode://file:can be confusing. While it might be technically valid in some URI parsers, a single slash is more conventional for absolute file paths and less likely to cause confusion. The formatvscode://file/path/to/fileis what's typically expected and is easier to read.