1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00
Commit Graph

22 Commits

Author SHA1 Message Date
Jack Worman
8e5904d624 Fix get_object_vars on enums 2022-12-21 22:51:53 -06:00
Marco Pivetta
ed2cde1b93 Mark Reflection(Method|Property)#setAccessible() as pure starting from PHP 8.1 onwards
This will highlight unused code.

Ref: https://github.com/php/php-src/pull/5412
Ref: https://wiki.php.net/rfc/make-reflection-setaccessible-no-op
Ref: https://github.com/php/php-src/pull/5411

Example https://3v4l.org/PNeeZ

```php
<?php

class Foo {
    private $bar = 'baz';
    private function taz() { return 'waz'; }
}

//var_dump((new ReflectionProperty(Foo::class, 'bar'))->getValue(new Foo));
//var_dump((new ReflectionMethod(Foo::class, 'taz'))->invoke(new Foo));
```

Produces (starting from PHP 8.1):

```
string(3) "baz"
string(3) "waz"
```
2022-12-07 14:22:15 +01:00
Marco Pivetta
93c5df6bfc Refine ReflectionUnionType and ReflectionIntersectionType for PHP 8.1 and PHP 8.2
* in PHP 8.0, `ReflectionUnionType` is composed on `ReflectionNamedType`s
* in PHP 8.1, `ReflectionIntersectionType` is composed of `ReflectionNamedType`s
* in PHP 8.2, `ReflectionUnionType` is composed of `ReflectionIntersectionType|ReflectionNamedType`s

Slight variations for each PHP version.

As per local testing, this doesn't work yet.

## Local testing setup:
I did some digging to make sure that the stubs work as expected.

Here's what I did to validate this patch locally (since I don't think it can really be fully automated)

## Create a dummy file to verify used symbols

```php
<?php

namespace Testing;

/** @return \ReflectionClass<\stdClass> */
function getAClass(): \ReflectionClass { throw new \Exception('irrelevant'); }
function getAnUnionType(): \ReflectionUnionType { throw new \Exception('irrelevant'); }
function getAnIntersectionType(): \ReflectionIntersectionType { throw new \Exception('irrelevant'); }

// verifying that `getName()` is stubbed in all versions: this should always be a `class-string<\stdClass>`
$name = getAClass()->getName();
// union types should appear starting with PHP 8.0. Starting with PHP 8.2, they allow for intersections.
$unionTypes = getAnUnionType()->getTypes();
// intersection types should appear starting with PHP 8.1
$intersectionTypes = getAnIntersectionType()->getTypes();

$results = [$name, $unionTypes, $intersectionTypes];

/** @psalm-trace $results */ // tracing this will show us the differences between versions
return $results;
```

## Run the script against various `vimeo/psalm` versions

```sh
docker run --rm -ti -v $(pwd):/app -w /app php:7.4 ./psalm --php-version=7.4 --no-cache reflection-test.php | grep Trace

docker run --rm -ti -v $(pwd):/app -w /app php:8.0 ./psalm --php-version=8.0 --no-cache reflection-test.php | grep Trace

docker run --rm -ti -v $(pwd):/app -w /app php:8.1 ./psalm --php-version=8.1 --no-cache reflection-test.php | grep Trace

docker run --rm -ti -v $(pwd):/app -w /app php:8.2.0RC7-cli ./psalm --php-version=8.2 --no-cache reflection-test.php | grep Trace

```

## Evaluate output

```
❯ docker run --rm -ti -v $(pwd):/app -w /app php:7.4 ./psalm --php-version=7.4 --no-cache reflection-test.php | grep Trace
ERROR: Trace - reflection-test.php:20:1 - $results: list{class-string<stdClass>, mixed, mixed} (see https://psalm.dev/224)

❯ docker run --rm -ti -v $(pwd):/app -w /app php:8.0 ./psalm --php-version=8.0 --no-cache reflection-test.php | grep Trace
ERROR: Trace - reflection-test.php:20:1 - $results: list{class-string<stdClass>, non-empty-list<ReflectionNamedType>, mixed} (see https://psalm.dev/224)

❯ docker run --rm -ti -v $(pwd):/app -w /app php:8.1 ./psalm --php-version=8.1 --no-cache reflection-test.php | grep Trace
ERROR: Trace - reflection-test.php:20:1 - $results: list{class-string<stdClass>, non-empty-list<ReflectionNamedType>, non-empty-list<ReflectionNamedType>} (see https://psalm.dev/224)

psalm on  feature/#8720-improve-types-and-purity-for-reflection-symbols [!?] via 🐘 v8.1.13 via ❄️  impure (nix-shell) took 4s
❯ docker run --rm -ti -v $(pwd):/app -w /app php:8.2.0RC7-cli ./psalm --php-version=8.2 --no-cache reflection-test.php | grep Trace
ERROR: Trace - reflection-test.php:20:1 - $results: list{class-string<stdClass>, non-empty-list<ReflectionNamedType>, non-empty-list<ReflectionNamedType>} (see https://psalm.dev/224)
```
2022-12-06 18:26:50 +01:00
Marco Pivetta
322cff6f43 Declaring more precise types and purity boundaries on ext-reflection symbols in .phpstub files
Also:

 * added PHP 8.2 stubs
 * refined types to make impossible scenarios more clear (like `ReflectionIntersectionType#allowsNull()`)

This is a first attempt at refining these types: the structure of these stubs is quite confusing to me,
so I don't know if this approach is correct, and if the stubs are merged together, or if entire symbols
need to be completely re-declared for each PHP version.
2022-12-06 11:08:30 +01:00
Bei Xiao
40cc346991 Update stub 2022-02-23 00:52:53 +02:00
orklah
0702a0b3e7 add ReflectionIntersectionType stub 2022-02-09 19:32:17 +01:00
Saif Eddin Gmati
40ab6551a4
fix(stubs): UnitEnum::cases() can return an empty list 2021-12-21 12:06:41 +01:00
Ricardo Boss
f0d7556200 Added pure annotations to enum functions 2021-12-20 23:20:50 +01:00
Bruce Weirdan
41256c74d1
Added enum-related stubs and callmaps
Fixes vimeo/psalm#6430
2021-11-28 11:41:43 +02:00
Bruce Weirdan
806db287d2
Infer ::from() and ::tryFrom() return types on backed enums
Fixes vimeo/psalm#6429
2021-11-28 09:47:01 +02:00
Ricardo Boss
f93c84b918
Made name property of UnitEnum readonly 2021-11-22 13:47:36 +01:00
Ricardo Boss
6d58a708fc
Make UnitEnum name property non-empty 2021-11-22 13:38:08 +01:00
orklah
b7e70f3cd7 load the ReturnTypeWillChange stubs for all versions to allow using it on every PHP version 2021-11-09 19:57:03 +01:00
orklah
8bf8036213 add ReturnTypeWillChange with attribute attribute 2021-10-24 23:21:57 +02:00
Tomas Norre Mikkelsen
dd49ac5975 Resolve forgotten merge conflict 2021-10-14 20:38:26 +02:00
Tomas Norre Mikkelsen
b39b523fb4 Convert pgsql functions to use object instead of a resource 2021-10-14 19:30:03 +02:00
Tomas Norre Mikkelsen
960af957db Convert PSpell resources to objects 2021-10-04 13:41:31 +02:00
Tomas Norre Mikkelsen
a49ff46448 Update typo in PHP81.phpstub 2021-10-04 12:58:01 +02:00
Tomas Norre Mikkelsen
993349d64d Convert LDAP resources to objects 2021-10-04 12:57:12 +02:00
Tomas Norre Mikkelsen
232ebb7e57 Convert imap functions to use object instead of a resource 2021-10-02 11:58:08 +02:00
Bruce Weirdan
08155dcb41
Added FTP\Connection class 2021-09-04 23:45:21 +03:00
Matthew Brown
832a190dd4
Support enums (#5699)
* Add initial enum preparation

* Support cases method

* Ignore bad use error

cc @weirdan

* Fix type
2021-05-03 17:54:09 -04:00