Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,13 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php: ['8.1', '8.2', '8.3', '8.4']
php: ['8.1', '8.2', '8.3', '8.4', '8.5']
deps: ['high', 'low', 'stable']
exclude:
- php: 8.4
deps: low
- php: 8.5
deps: low

fail-fast: false
steps:
Expand Down
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
],
"require": {
"php": "^8",
"composer-runtime-api": "^2",
"psalm/psalm-plugin-api": "^0.4",
"vimeo/psalm": "^7|dev-master"
},
Expand All @@ -39,9 +40,10 @@
"codeception/codeception": "^5.0",
"doctrine/coding-standard": "^9.0",
"doctrine/collections": "^1.8 || ^2.0",
"doctrine/doctrine-bundle": "^1.11 || ^2.0",
"doctrine/orm": "^2.6",
"doctrine/persistence": "^2.0",
"doctrine/dbal": "^2.13 || ^3.0 || ^4.0",
"doctrine/doctrine-bundle": "^1.11 || ^2.0 || ^3.0",
"doctrine/orm": "^2.6 || ^3.0",
"doctrine/persistence": "^2.0 || ^3.0 || ^4.0",
"phly/keep-a-changelog": "^2.1",
"phpunit/phpunit": "^10.5 || ^11.5 || ^12.0 || ^13.0",
"squizlabs/php_codesniffer": "^3.3",
Expand Down
51 changes: 50 additions & 1 deletion src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Weirdan\DoctrinePsalmPlugin;

use Composer\InstalledVersions;
use OutOfBoundsException;
use Override;
use Psalm\Plugin\PluginEntryPointInterface;
use Psalm\Plugin\RegistrationInterface;
Expand All @@ -11,10 +13,24 @@
use function array_merge;
use function class_exists;
use function glob;
use function strpos;
use function version_compare;

/** @psalm-api */
class Plugin implements PluginEntryPointInterface
{
/**
* The stubs of the versions of a package they describe, loaded when that version is installed: directory of
* stubs => [package, operator of version_compare(), version]
*/
private const VERSIONED_STUBS = [
'DBAL/before-3' => ['doctrine/dbal', '<', '3.0'],
'DBAL/since-3' => ['doctrine/dbal', '>=', '3.0'],
'DBAL/before-4' => ['doctrine/dbal', '<', '4.0'],
'DBAL/since-4' => ['doctrine/dbal', '>=', '4.0'],
'ORM/before-3' => ['doctrine/orm', '<', '3.0'],
];

#[Override]
public function __invoke(RegistrationInterface $psalm, ?SimpleXMLElement $config = null): void
{
Expand All @@ -34,9 +50,42 @@ public function __invoke(RegistrationInterface $psalm, ?SimpleXMLElement $config
*/
private function getStubFiles(): array
{
return array_merge(
$files = array_merge(
glob(__DIR__ . '/../stubs/*.phpstub') ?: [],
glob(__DIR__ . '/../stubs/DBAL/*.phpstub') ?: []
);

foreach (self::VERSIONED_STUBS as $directory => [$package, $operator, $version]) {
if (self::isInstalled($package, $operator, $version)) {
$files = array_merge($files, glob(__DIR__ . '/../stubs/' . $directory . '/*.phpstub') ?: []);
}
}

return $files;
}

/**
* Whether the installed version of $package compares to $version with $operator. Without Composer's metadata, or
* for a branch that isn't numbered, the newest version is assumed.
*
* @param '<'|'>=' $operator
*/
private static function isInstalled(string $package, string $operator, string $version): bool
{
$installed = null;

if (class_exists(InstalledVersions::class)) {
try {
$installed = InstalledVersions::getVersion($package);
} catch (OutOfBoundsException $e) {
}
}

// a dev branch is normalized as `dev-name`, a numbered one as `4.7.9999999.9999999-dev`
if ($installed === null || strpos($installed, 'dev-') === 0) {
return $operator === '>=';
}

return version_compare($installed, $version, $operator);
}
}
2 changes: 1 addition & 1 deletion src/Provider/ReturnTypeProvider/CollectionFirstAndLast.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static function getMethodReturnType(MethodReturnTypeProviderEvent $event)
}

$childNode = $type->type_params;
if (! isset($childNode[1]) || ! $childNode[1] instanceof Type\Union) {
if (! isset($childNode[1])) {
return null;
}

Expand Down
57 changes: 57 additions & 0 deletions stubs/AbstractQuery.phpstub
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
// phpcs:ignoreFile
namespace Doctrine\ORM;

/**
* What a database holds may have been written by other requests (stored input): the rows and scalars a query reads
* are input. The entities it hydrates carry what their properties are given.
*/
abstract class AbstractQuery
{
/**
* Gets the array of results for the query.
*
* Alias for execute(null, HYDRATE_ARRAY).
*
* @return mixed[]
*
* @psalm-taint-source input
*/
public function getArrayResult() {}

/**
* Gets one-dimensional array of results for the query.
*
* Alias for execute(null, HYDRATE_SCALAR_COLUMN).
*
* @return mixed[]
*
* @psalm-taint-source input
*/
public function getSingleColumnResult() {}

/**
* Gets the scalar results for the query.
*
* Alias for execute(null, HYDRATE_SCALAR).
*
* @return mixed[]
*
* @psalm-taint-source input
*/
public function getScalarResult() {}

/**
* Gets the single scalar result of the query.
*
* Alias for getSingleResult(HYDRATE_SINGLE_SCALAR).
*
* @return bool|float|int|string|null The scalar result.
*
* @throws NoResultException If the query returned no result.
* @throws NonUniqueResultException If the query result is not unique.
*
* @psalm-taint-source input
*/
public function getSingleScalarResult() {}
}
70 changes: 35 additions & 35 deletions stubs/Collections.phpstub
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ use ArrayAccess;

/**
* @template TKey as array-key
* @template TValue
* @template T
*
* @template-extends ReadableCollection<TKey,TValue>
* @template-extends ArrayAccess<TKey|null,TValue>
* @template-extends ReadableCollection<TKey,T>
* @template-extends ArrayAccess<TKey|null,T>
*/
interface Collection extends ReadableCollection, ArrayAccess
{

/**
* @param ?TKey $offset
* @param TValue $value
* @param T $value
* @return void
*/
public function offsetSet($offset, $value);

/**
* @param TValue $element
* @param T $element
* @return true
*/
public function add($element);
Expand All @@ -37,47 +37,47 @@ interface Collection extends ReadableCollection, ArrayAccess

/**
* @param TKey $key
* @return TValue|null
* @return T|null
*/
public function remove($key);

/**
* @param TValue $element
* @param T $element
* @return bool
*/
public function removeElement($element);

/**
* @param TKey $key
* @param TValue $value
* @param T $value
* @return void
*/
public function set($key, $value);

/**
* @param Closure(TValue=):bool $p
* @return Collection<TKey,TValue>
* @param Closure(T=):bool $p
* @return Collection<TKey,T>
*/
public function filter(Closure $p);

/**
* @param Closure(TKey=,TValue=):bool $p
* @return array{0:Collection<TKey,TValue>,1:Collection<TKey,TValue>}
* @param Closure(TKey=,T=):bool $p
* @return array{0:Collection<TKey,T>,1:Collection<TKey,T>}
*/
public function partition(Closure $p);
}

/**
* @template TKey as array-key
* @template-covariant TValue
* @template-covariant T
*
* @template-extends IteratorAggregate<TKey,TValue>
* @template-extends IteratorAggregate<TKey,T>
*/
interface ReadableCollection extends Countable, IteratorAggregate
{

/**
* @param TValue $element
* @param T $element
* @return bool
*/
public function contains($element);
Expand All @@ -96,7 +96,7 @@ interface ReadableCollection extends Countable, IteratorAggregate

/**
* @param TKey $key
* @return TValue|null
* @return T|null
*/
public function get($key);

Expand All @@ -106,22 +106,22 @@ interface ReadableCollection extends Countable, IteratorAggregate
public function getKeys();

/**
* @return list<TValue>
* @return list<T>
*/
public function getValues();

/**
* @return array<TKey,TValue>
* @return array<TKey,T>
*/
public function toArray();

/**
* @return TValue|false
* @return T|false
*/
public function first();

/**
* @return TValue|false
* @return T|false
*/
public function last();

Expand All @@ -131,68 +131,68 @@ interface ReadableCollection extends Countable, IteratorAggregate
public function key();

/**
* @return TValue|false
* @return T|false
*/
public function current();

/**
* @return TValue|false
* @return T|false
*/
public function next();

/**
* @param int $offset
* @param int|null $length
* @return array<TKey,TValue>
* @return array<TKey,T>
*/
public function slice($offset, $length = null);

/**
* @param Closure(TKey=,TValue=):bool $p
* @param Closure(TKey=,T=):bool $p
* @return bool
*/
public function exists(Closure $p);

/**
* @param Closure(TValue=):bool $p
* @return ReadableCollection<TKey,TValue>
* @param Closure(T=):bool $p
* @return ReadableCollection<TKey,T>
*/
public function filter(Closure $p);

/**
* @template T
* @param Closure(TValue=):T $func
* @return Collection<TKey,T>
* @template U
* @param Closure(T=):U $func
* @return Collection<TKey,U>
*/
public function map(Closure $func);

/**
* @param Closure(TKey=,TValue=):bool $p
* @return array{0:ReadableCollection<TKey,TValue>,1:ReadableCollection<TKey,TValue>}
* @param Closure(TKey=,T=):bool $p
* @return array{0:ReadableCollection<TKey,T>,1:ReadableCollection<TKey,T>}
*/
public function partition(Closure $p);

/**
* @param Closure(TKey=,TValue=):bool $p
* @param Closure(TKey=,T=):bool $p
* @return bool
*/
public function forAll(Closure $p);

/**
* @param TValue $element
* @param T $element
* @return false|TKey
*/
public function indexOf($element);
}

/**
* @template TKey as array-key
* @template TValue
* @template T
*/
interface Selectable
{
/**
* @return Collection<TKey,TValue>
* @return Collection<TKey,T>
*/
public function matching(Criteria $criteria);
}
Loading
Loading