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 a stub for Criteria #125

Open
wants to merge 8 commits into
base: 2.x
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions stubs/Collections.phpstub
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ interface Collection extends Countable, IteratorAggregate, ArrayAccess
interface Selectable
{
/**
* @psalm-mutation-free
* @return Collection<TKey,TValue>
*/
public function matching(Criteria $criteria);
Expand Down
24 changes: 24 additions & 0 deletions stubs/Criteria.phpstub
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Doctrine\Common\Collections;

class Criteria
{
public const ASC = 'ASC';
public const DESC = 'DESC';

/**
* @psalm-pure
*/
public static function create()
{
}

/**
* @psalm-external-mutation-free
* @param array<string, Criteria::ASC|Criteria::DESC>
*/
public function orderBy(array $orderings)
{
}
}
58 changes: 58 additions & 0 deletions tests/acceptance/Criteria.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
Feature: Criteria
In order to use Doctrine Criteria
As a Psalm user
I need Psalm to typecheck Criteria

Background:
Given I have Doctrine plugin enabled
And I have the following code preamble
"""
<?php
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\Common\Collections\Selectable;
"""
# Psalm enables cache when there's a composer.lock file
And I have empty composer.lock

@Criteria::create
Scenario: Creating a Criteria in a pure context
Given I have the following code
"""
/** @psalm-pure */
function foo(Selectable $collection): Collection
{
return $collection->matching(Criteria::create());
}
"""
When I run Psalm
Then I see no errors

@Criteria::orderBy
Scenario: Order a Criteria in a pure context
Given I have the following code
"""
/** @psalm-pure */
function foo(Selectable $collection): Collection
{
return $collection->matching(Criteria::create()->orderBy(['foo' => Criteria::ASC]));
}
"""
When I run Psalm
Then I see no errors

@Criteria::orderBy
Scenario: Invalid sorting provided to matching
Given I have the following code
"""
/** @psalm-pure */
function foo(Selectable $collection): Collection
{
return $collection->matching(Criteria::create()->orderBy(['foo' => 'bar']));
}
"""
When I run Psalm
Then I see these errors
| Type | Message |
| todo | todo |
And I see no other errors