Skip to content

Commit

Permalink
Upgrade phpstan and psalm, add stubs for \Pgsql\* classes in PHP < 8.1
Browse files Browse the repository at this point in the history
Also prevent psalm from analyzing the actual constant used for tests configuration by feeding it a stub instead, see vimeo/psalm#4024
  • Loading branch information
sad-spirit committed Nov 18, 2021
1 parent 35d7922 commit 2932de4
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/.github export-ignore
/.gitignore export-ignore
/ci export-ignore
/pgsql-stubs.php export-ignore
/phpstan.neon export-ignore
/phpstan-baseline.neon export-ignore
/psalm.xml export-ignore
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
"ext-pgsql": "*",
"phpunit/phpunit": "^8.0|^9.0",
"psr/cache": "^1.0",
"phpstan/phpstan": "^0.12.86",
"vimeo/psalm": "^4.7.2"
"phpstan/phpstan": "^1.1",
"vimeo/psalm": "^4.12"
},
"autoload": {
"psr-4": {
Expand Down
21 changes: 21 additions & 0 deletions pgsql-stubs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* Stubs for resource replacing classes introduced in PHP 8.1
*/

namespace Pgsql;

if (\PHP_VERSION_ID < 80100) {
final class Connection
{
}

final class Result
{
}

final class Lob
{
}
}
3 changes: 2 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
includes:
- phpstan-baseline.neon
- phpstan-baseline.neon

parameters:
level: 6
Expand All @@ -8,6 +8,7 @@ parameters:
- tests
bootstrapFiles:
- tests/config.php.dist
- pgsql-stubs.php
dynamicConstantNames:
- TESTS_SAD_SPIRIT_PG_WRAPPER_CONNECTION_STRING
treatPhpDocTypesAsCertain: false
Expand Down
24 changes: 14 additions & 10 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,29 @@
</projectFiles>

<issueHandlers>
<RedundantCondition>
<errorLevel type="suppress">
<!--
Documentation for pg_escape_literal() and pg_escape_identifier() is incorrect,
those can return false, not only string
-->
<file name="src/sad_spirit/pg_wrapper/Connection.php"/>
</errorLevel>
</RedundantCondition>
<InvalidCatch>
<errorLevel type="suppress">
<!-- Doesn't like catching \Psr\Cache\InvalidArgumentException that does not extend \Throwable -->
<file name="src/sad_spirit/pg_wrapper/converters/DefaultTypeConverterFactory.php"/>
<referencedClass name="\Psr\Cache\InvalidArgumentException"/>
</errorLevel>
</InvalidCatch>

<!-- Ignore these in tests, whatever we access is guaranteed to be not null -->
<PossiblyNullArrayAccess>
<errorLevel type="suppress">
<directory name="tests"/>
</errorLevel>
</PossiblyNullArrayAccess>
<PossiblyNullArgument>
<errorLevel type="suppress">
<directory name="tests"/>
</errorLevel>
</PossiblyNullArgument>
</issueHandlers>

<stubs>
<file name="tests/config.php.dist" />
<file name="tests/config.php.psalm" />
<file name="pgsql-stubs.php" />
</stubs>
</psalm>
5 changes: 5 additions & 0 deletions tests/config.php.psalm
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

// This should prevent psalm from ASSuming something about the constant's value.
// Until https://github.com/vimeo/psalm/issues/4024 is implemented
define('TESTS_SAD_SPIRIT_PG_WRAPPER_CONNECTION_STRING', mt_rand(1, 10) > 5 ? 'a string' : false);

0 comments on commit 2932de4

Please sign in to comment.