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

FUN-289 add phpstan annotations #3

Merged
merged 2 commits into from
Oct 9, 2020
Merged

FUN-289 add phpstan annotations #3

merged 2 commits into from
Oct 9, 2020

Conversation

Erikvv
Copy link
Contributor

@Erikvv Erikvv commented Sep 7, 2020

Dependencies

none

Deploy instructions

Minor tag

Whats this?

Like vcn/pipette#10 (please also review)

Add phpstan annotations so that some type errors can be detected in the usage of the library.

It does not work in all situations.

Test

Example:

phpstan analyse --level=6 tmp.php

<?php // tmp.php

use Vcn\Lib\EnumTest\Fruit;
use Vcn\Lib\EnumTest\Color;

require __DIR__ . '/vendor/autoload.php';

function takeInt(int $int): void {}

$banana = Fruit::BANANA();

// Parameter #1 $b of method Vcn\Lib\Enum::equals() expects Vcn\Lib\EnumTest\Fruit, Vcn\Lib\EnumTest\Color given.
$banana->equals(Color::BLUE());

// Parameter #1 $a of method Vcn\Lib\Enum::when() expects Vcn\Lib\EnumTest\Fruit, Vcn\Lib\EnumTest\Color given.
$matcher = $banana->when(Color::BLUE(), 'piet');

$matcher = $banana
    ->when(Fruit::BANANA(), 1)
    // Parameter #1 $enum of method Vcn\Lib\Enum\Matcher<Vcn\Lib\EnumTest\Fruit,int>::when() expects Vcn\Lib\EnumTest\Fruit, Vcn\Lib\EnumTest\Color given.
    ->when(Color::BLUE(), 2);

$stdClass = $banana
    ->when(Fruit::BANANA(), new stdClass())
    ->when(Fruit::APPLE(), new stdClass())
    ->get();

// Parameter #1 $int of function takeInt expects int, stdClass given.
takeInt($stdClass);

$stdClassOrInt = $banana
    ->when(Fruit::BANANA(), new stdClass())
    // false positive if you actually want a sum type
    // Parameter #2 $value of method Vcn\Lib\Enum\Matcher<Vcn\Lib\EnumTest\Fruit,stdClass>::when() expects stdClass, int given.
    ->when(Fruit::APPLE(), 42)
    ->get();

$stdClass2 = $banana
    ->whenDo(Fruit::BANANA(), fn (): stdClass => new stdClass())
    ->whenDo(Fruit::APPLE(), fn (): stdClass => new stdClass())
    ->get();

// Parameter #1 $int of function takeInt expects int, stdClass given.
takeInt($stdClass2);

$banana
    ->when(Fruit::BANANA(), new stdClass())
    // Parameter #1 $value of method Vcn\Lib\Enum\Matcher<Vcn\Lib\EnumTest\Fruit,stdClass>::orElse() expects stdClass, int given.
    ->orElse(42);

$stdClassOrInt2 = $banana
    ->when(Fruit::BANANA(), new stdClass())
    // no error here :(
    ->orElseDo(fn(): int => 42);

// Parameter #1 $int of function takeInt expects int, stdClass given.
takeInt($stdClassOrInt2);

/** @phpstan-var  \Vcn\Lib\Enum\Matcher<Fruit, Exception> $matcher */
$matcher = $banana
    ->when(Fruit::BANANA(), new LogicException())
    ->when(Fruit::APPLE(), new InvalidArgumentException());

// Parameter #1 $int of function takeInt expects int, Exception given.
takeInt($matcher->get());

tmp.php Outdated
@@ -0,0 +1 @@
<?php
Copy link

Choose a reason for hiding this comment

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

Heeft het committen van dit lege bestand naar de repo meerwaarde?

@Erikvv Erikvv merged commit e45fd51 into master Oct 9, 2020
@gerritdrost gerritdrost mentioned this pull request Dec 18, 2020
@sudo-plz sudo-plz deleted the FUN-289-phpstan branch July 9, 2021 12:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants