Skip to content

Conversation

@shmax
Copy link
Contributor

@shmax shmax commented Sep 2, 2023

Adding a PHPStan extension so that the various return types of Model::find can be statically checked.

In other words, if you're a PHPStan user, the following snippets will now all pass analysis without errors:

/**
 * Static checking for single model
 */
$book = Book::find(1);
assert($book instanceof Book);

$book = Book::find(['name'=>'Ubik']);
assert($book instanceof Book);

/**
 * Static checking for nullable single model
 */
$book = Book::find('first', ['name'=>'Waldo']);
assert(is_null($book));

$book = Book::find('last', ['name'=>'Waldo']);
assert(is_null($book));

/**
 * Static checking for array of models
 */
$books = Book::find(
    'all',
    ['name' => 'William']
);
assert(0==count($books));

$books = Book::find(1, 3, 8);
assert(0==count($books));

$books = Book::find([1, 3, 8]);
assert(0==count($books));

$books = Book::find(['conditions'=> ['name' => 'Kurt']]);
assert(0==count($books));

@shmax shmax merged commit 6118ff0 into master Sep 2, 2023
@shmax shmax deleted the stan-dynamic branch September 2, 2023 21:52
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.

2 participants