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

Psalm infers > 100% type coverage #6270

Closed
DaveLiddament opened this issue Aug 9, 2021 · 6 comments
Closed

Psalm infers > 100% type coverage #6270

DaveLiddament opened this issue Aug 9, 2021 · 6 comments

Comments

@DaveLiddament
Copy link
Contributor

To recreate. Run Psalm on the the SARB codebase. It reports for me

Psalm was able to infer types for 100.0181% of the codebase

Running with --stats all files are 100% apart from this one, which is 103%.

103% src/Domain/Utils/ArrayUtils.php (-1 mixed)

I've created a branch with copies of ArrayUtils.php to help track down the issue further...

I made a copy of ArrayUtils.php called ArrayUtils2.php and removed methods one at a time. Each time I'd run:

vendor/bin/psalm --stats --no-cache | grep ArrayUtils2

Trimming down ArrayUtils2.php to this:

<?php

declare(strict_types=1);

namespace DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Utils;

class ArrayUtils2
{
    /**
     * @param mixed $entity
     *
     * @throws ArrayParseException
     */
    public static function assertArray($entity): void
    {
        if (!is_array($entity)) {
            throw ArrayParseException::invalidType('base level', 'array');
        }
    }

}

I got 200% type coverage:

200% src/Domain/Utils/ArrayUtils2.php (-1 mixed)

I tried other combinations of the method (to see if the issue was with throwing an exception) and I am even more confused:

See ArrayUtils3.php

class ArrayUtils3
{
    /**
     * @param mixed $entity
     *
     * @throws Exception
     */
    public static function assertArray($entity): void
    {
        if (!is_array($entity)) {
            throw new Exception();
        }
    }
}

And ArrayUtils4.php

class ArrayUtils4
{
    /**
     * @param mixed $entity
     *
     * @throws Exception
     */
    public static function assertArray($entity): void
    {
        if (!is_array($entity)) {
            throw new Exception('A message');
        }
    }
}

Running vendor/bin/psalm --no-cache --stats | grep ArrayUtils I get:

103% src/Domain/Utils/ArrayUtils.php (-1 mixed)
200% src/Domain/Utils/ArrayUtils2.php (-1 mixed)
0% src/Domain/Utils/ArrayUtils3.php (-1 mixed)

NOTE there is no record of src/Domain/Utils/ArrayUtils4.php

Let me know if there is any other info I can provide you with to help debug.


Versions of Psalms I've tried this on (both give same result):

Psalm 4.6.1@e93e532e4eaad6d68c4d7b606853800eaceccc72
Psalm 4.7.2@83a0325c0a95c0ab531d6b90c877068b464377b5
PHP 8.0.3 (cli) (built: Mar  5 2021 08:36:11) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.3, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.3, Copyright (c), by Zend Technologies
    with Xdebug v3.0.3, Copyright (c) 2002-2021, by Derick Rethans
@psalm-github-bot
Copy link

Hey @DaveLiddament, can you reproduce the issue on https://psalm.dev ?

@weirdan
Copy link
Collaborator

weirdan commented Aug 9, 2021

Would you mind re-running your test with latest stable (4.9.2) and master? Just to rule out any possible fixes that may have happened since (although I don't think there were any).

@DaveLiddament
Copy link
Contributor Author

@weirdan

I still get the same issues on 4.9.2.

vagrant@sarb:/vagrant$ vendor/bin/psalm -v
Psalm 4.9.2@00c062267d6e3229d91a1939992987e2d46f2393
vagrant@sarb:/vagrant$ vendor/bin/psalm --no-cache --stats | grep Arra
Scanning files...
Analyzing files...

░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░  60 / 202 (29%)
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 120 / 202 (59%)
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 180 / 202 (89%)
░░░░░░░░░░░░░░░░░░░░░░
100% src/Domain/Utils/ArrayParseException.php (0 mixed)
103% src/Domain/Utils/ArrayUtils.php (-1 mixed)
200% src/Domain/Utils/ArrayUtils2.php (-1 mixed)
0% src/Domain/Utils/ArrayUtils3.php (-1 mixed)

My lunch break is over, but I'll try and recreate in a standalone project after work today.

@DaveLiddament
Copy link
Contributor Author

@weirdan I've recreated issue in a standalone project here

Results from Psalm:

vendor/bin/psalm --stats --no-cache
Scanning files...
Analyzing files...

░░░░░░░░
------------------------------
No errors found!
------------------------------

Checks took 0.66 seconds and used 43.192MB of memory
Psalm was able to infer types for 114.2857% of the codebase
-----------------
100% src/ArrayParseException.php (0 mixed)
103% src/ArrayUtils.php (-1 mixed)
200% src/ArrayUtils2.php (-1 mixed)
0% src/ArrayUtils3.php (-1 mixed)
0% src/ArrayUtils5.php (-1 mixed)

Also there is no type coverage reported for:

  • src/ArrayUtils4.php (which is the same as src/ArrayUtils3.php but in src/ArrayUtils4.php a message is passed into the exception.
  • src/ArrayUtils6.php (which is the same as src/ArrayUtils5.php but in src/ArrayUtils6.php a message is passed into the exception.

Seems to get different results depending on how and which exception is thrown if the is_array check returns false.

@orklah
Copy link
Collaborator

orklah commented Aug 10, 2021

Related to #3660

@muglug
Copy link
Collaborator

muglug commented Aug 14, 2021

Dupe of #3660, closing (but thanks for the info, it led me to the culprit).

@muglug muglug closed this as completed Aug 14, 2021
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

No branches or pull requests

4 participants