Skip to content
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

Add generic annotations to EventInterface #32

Merged
merged 22 commits into from
Dec 10, 2022
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
95042a3
Add PHPStan generic annotations to EventInterface
villermen Nov 10, 2022
38d0eb7
Replace PHPStan annotations with Psalm ones
villermen Nov 11, 2022
27a21ee
Correct Psalm this-out syntax
villermen Nov 13, 2022
c0caafe
Use covariant templating, address/suppress static issues and add note…
villermen Nov 14, 2022
354b6f6
Change default value for event params to better infer default type
villermen Nov 14, 2022
bbef138
Check parameter key type in Event::getParam()
villermen Nov 14, 2022
9ae588b
Add directory and initial check for explicit Psalm template verification
villermen Nov 22, 2022
69e3af7
Remove key-of type from EventInterface::getParam()
villermen Nov 22, 2022
e781354
Correct type of Event::$name, replace empty() with explicit empty arr…
villermen Nov 22, 2022
9e6304d
Add static type check for empty Event ctor
villermen Nov 22, 2022
d462dc2
Appease PHPCS multiline comment rule
villermen Nov 22, 2022
8607e14
Remove obsolete suppression on getParam() in EventTest
villermen Dec 6, 2022
1fd8d60
Fix suppressions for new Psalm 5 false-positives
villermen Dec 6, 2022
184b112
Replace redundant (sometimes wrong) docblocks on Event with inheritDoc
villermen Dec 6, 2022
fe4ab09
Include psalm directory in PHPCS checks, fix reported errors
villermen Dec 6, 2022
5c72ee3
Add psalm directory to export ignore list
villermen Dec 6, 2022
2a4c60a
Fix newlines in Psalm checks
villermen Dec 6, 2022
5fc158f
Add Psalm checks for all implemented/not implementable event function…
villermen Dec 6, 2022
85540cb
Add (failing) checks for inherited psalm-this-out annotations, respec…
villermen Dec 7, 2022
9dc446d
Try fixing psalm-this-out inheritance by adding static to the type
villermen Dec 8, 2022
dccf3f6
Fix EventChecks::checkThisOutInheritance() array notation
villermen Dec 10, 2022
94f2f0c
Check individual target and params values too
villermen Dec 10, 2022
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
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
/phpbench.json export-ignore
/phpunit.xml.dist export-ignore
/test/ export-ignore
/psalm/ export-ignore
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"php": "8.0.99"
},
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
"dealerdirect/phpcodesniffer-composer-installer": true,
"composer/package-versions-deprecated": true
}
},
"extra": {
Expand Down Expand Up @@ -52,7 +53,8 @@
"autoload-dev": {
"psr-4": {
"LaminasTest\\EventManager\\": "test/",
"LaminasBench\\EventManager\\": "benchmarks/"
"LaminasBench\\EventManager\\": "benchmarks/",
"LaminasPsalm\\EventManager\\": "psalm/"
}
},
"scripts": {
Expand Down
1 change: 1 addition & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

<!-- Paths to check -->
<file>benchmarks</file>
<file>psalm</file>
<file>src</file>
<file>test</file>

Expand Down
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<projectFiles>
<directory name="src"/>
<directory name="test"/>
<directory name="psalm"/>
<ignoreFiles>
<directory name="vendor"/>
</ignoreFiles>
Expand Down
9 changes: 9 additions & 0 deletions psalm/CheckObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace LaminasPsalm\EventManager;

class CheckObject
{
}
50 changes: 50 additions & 0 deletions psalm/EventChecks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace LaminasPsalm\EventManager;

use Laminas\EventManager\Event;
use Laminas\EventManager\EventInterface;

class EventChecks
{
/**
* @return array{
* Event,
* EventInterface,
* Event<null, array<empty, empty>>,
* EventInterface<null, array<empty, empty>>,
* }
*/
public function checkEmptyCtorInference(): array
{
$event = new Event();
return [
$event,
$event,
$event,
$event,
];
}

/**
* @return array{
* Event<'target-string', array{foo: 'bar', baz: true}>,
* 'target-string',
* array{foo: 'bar', baz: true},
* }
*/
public function checkCtorInference(): array
{
$event = new Event(null, 'target-string', [
'foo' => 'bar',
'baz' => true,
]);
return [
$event,
$event->getTarget(),
$event->getParams(),
];
}
}
96 changes: 96 additions & 0 deletions psalm/EventInterfaceChecks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

declare(strict_types=1);

namespace LaminasPsalm\EventManager;

use Laminas\EventManager\EventInterface;

class EventInterfaceChecks
{
/**
* @param EventInterface<CheckObject, array{foo: int, bar: CheckObject}> $e
* @return array{
* CheckObject,
* array{foo: int, bar: CheckObject},
* int,
* CheckObject
* }
*/
public function checkTargetAndParamsMatchTemplate(EventInterface $e): array
{
return [
$e->getTarget(),
$e->getParams(),
$e->getParams()['foo'],
$e->getParams()['bar'],
];
}

/**
* @param EventInterface<null, array<empty, empty>> $e
* @return array{
* EventInterface<CheckObject, array<empty, empty>>,
* }
*/
public function checkSetTargetChangesTemplate(EventInterface $e): array
{
$e->setTarget(new CheckObject());
villermen marked this conversation as resolved.
Show resolved Hide resolved
return [
$e,
];
}

/**
* @param EventInterface<null, array{foo: int}> $e
* @return array{
* EventInterface<null, array{foo: CheckObject, bar: 'baz'}>,
* }
*/
public function checkSetParamsChangesTemplate(EventInterface $e): array
{
$e->setParams([
'foo' => new CheckObject(),
'bar' => 'baz',
]);
return [
$e,
];
}

/**
* Individual params obtained via `getParam()` can't be inferred because their keys/values can't be selected from
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good enough though 👍

* the template type.
*
* @param EventInterface<null, array{foo: int, bar: CheckObject}> $e
* @return array{
* mixed,
* mixed
* }
*/
public function checkIndividualParamsNotInferred(EventInterface $e): array
{
return [
$e->getParam('foo'),
$e->getParam('bar'),
];
}

/**
* Changing the template and statically checking individual values is not possible with Psalm because
* key-of and value-of do not work on objects.
*
* @param EventInterface<null, array{foo: int}> $e
* @return array{
* EventInterface<null, array{foo: int}>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still kinda weird: fixable somehow? 🤔

See my reverts from laminas/laminas-stdlib#78

* }
*/
public function checkIndividualParamDoesNotChangeTemplate(EventInterface $e): array
{
$e->setParam('foo', 'notAnInt');
$e->setParam('bar', 'keyDidNotExist');
return [
$e,
];
}
}
Loading