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

Allow multiple / attribute usages on the same method #634

Merged
merged 3 commits into from
Feb 14, 2025
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Changed

- Allow multiple
`Ignore_Deprecation`/`Ignore_Incorrect_Usage`/`Expected_Deprecation`/`Expected_Incorrect_Usage`
attributes on a test method/class.

## v1.5.2 - 2025-02-06

### Fixed
Expand Down
85 changes: 85 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
parameters:
ignoreErrors:
-
message: '#^Instanceof between TModel of Mantle\\Database\\Model\\Model and Mantle\\Database\\Model\\Model will always evaluate to true\.$#'
identifier: instanceof.alwaysTrue
count: 2
path: src/mantle/database/query/class-collection.php

-
message: '#^Instanceof between static\(Mantle\\Database\\Query\\Collection\<TKey of \(int\|string\), TMapValue\>\) and Mantle\\Database\\Query\\Collection will always evaluate to true\.$#'
identifier: instanceof.alwaysTrue
count: 1
path: src/mantle/database/query/class-collection.php

-
message: '#^Instanceof between static\(Mantle\\Database\\Query\\Collection\<TMapWithKeysKey of \(int\|string\), TMapWithKeysValue\>\) and Mantle\\Database\\Query\\Collection will always evaluate to true\.$#'
identifier: instanceof.alwaysTrue
count: 1
path: src/mantle/database/query/class-collection.php

-
message: '#^Method Mantle\\Database\\Query\\Collection\:\:zip\(\) should return static\(Mantle\\Database\\Query\\Collection\<int, static\(Mantle\\Database\\Query\\Collection\<TKey of \(int\|string\), \(TModel of Mantle\\Database\\Model\\Model\)\|TZipValue\>\)\>\) but returns Mantle\\Support\\Collection\<int, Mantle\\Support\\Collection\<\(int\|string\), mixed\>\>\.$#'
identifier: return.type
count: 1
path: src/mantle/database/query/class-collection.php

-
message: '#^Method Mantle\\Support\\Collection\:\:chunk\(\) should return static\(Mantle\\Support\\Collection\<int, static\(Mantle\\Support\\Collection\<TKey of \(int\|string\), TValue\>\)\>\) but returns static\(Mantle\\Support\\Collection\<int, static\(Mantle\\Support\\Collection\<TKey of \(int\|string\), TValue\>\)\>\)\.$#'
identifier: return.type
count: 1
path: src/mantle/support/class-collection.php

-
message: '#^Method Mantle\\Support\\Collection\:\:combine\(\) should return static\(Mantle\\Support\\Collection\<TValue, TCombineValue\>\) but returns static\(Mantle\\Support\\Collection\<\(int\|string\), TValue\>\)\.$#'
identifier: return.type
count: 1
path: src/mantle/support/class-collection.php

-
message: '#^Method Mantle\\Support\\Collection\:\:filter\(\) should return static\(Mantle\\Support\\Collection\<TKey of \(int\|string\), TValue\>\) but returns static\(Mantle\\Support\\Collection\<TKey of \(int\|string\), TValue of mixed\>\)\.$#'
identifier: return.type
count: 1
path: src/mantle/support/class-collection.php

-
message: '#^Method Mantle\\Support\\Collection\:\:from\(\) should return static\(Mantle\\Support\\Collection\<TKeyFrom of \(int\|string\), TValueFrom\>\) but returns static\(Mantle\\Support\\Collection\<int, Mantle\\Database\\Model\\Post\|null\>\)\.$#'
identifier: return.type
count: 1
path: src/mantle/support/class-collection.php

-
message: '#^Method Mantle\\Support\\Collection\:\:group_by\(\) should return static\(Mantle\\Support\\Collection\<\(int\|string\), static\(Mantle\\Support\\Collection\<\(int\|string\), TValue\>\)\>\) but returns static\(Mantle\\Support\\Collection\<\(int\|string\), static\(Mantle\\Support\\Collection\<\*NEVER\*, \*NEVER\*\>\)\>\)\.$#'
identifier: return.type
count: 1
path: src/mantle/support/class-collection.php

-
message: '#^Method Mantle\\Support\\Collection\:\:map_to_dictionary\(\) should return static\(Mantle\\Support\\Collection\<TMapToDictionaryKey of \(int\|string\), array\<int, TMapToDictionaryValue\>\>\) but returns static\(Mantle\\Support\\Collection\<TMapToDictionaryKey of \(int\|string\), non\-empty\-list\<TMapToDictionaryValue\>\>\)\.$#'
identifier: return.type
count: 1
path: src/mantle/support/class-collection.php

-
message: '#^Method Mantle\\Support\\Collection\:\:only_children\(\) should return static\(Mantle\\Support\\Collection\<TKey of \(int\|string\), array\>\) but returns static\(Mantle\\Support\\Collection\<TKey of \(int\|string\), TValue\>\)\.$#'
identifier: return.type
count: 1
path: src/mantle/support/class-collection.php

-
message: '#^Method Mantle\\Support\\Collection\:\:partition\(\) should return static\(Mantle\\Support\\Collection\<int\<0, 1\>, static\(Mantle\\Support\\Collection\<TKey of \(int\|string\), TValue\>\)\>\) but returns static\(Mantle\\Support\\Collection\<int, static\(Mantle\\Support\\Collection\<\(int\|string\), mixed\>\)\>\)\.$#'
identifier: return.type
count: 1
path: src/mantle/support/class-collection.php

-
message: '#^Method Mantle\\Support\\Collection\:\:times\(\) should return static\(Mantle\\Support\\Collection\<int, TTimesValue\>\) but returns static\(Mantle\\Support\\Collection\<int, int\<1, max\>\>\)\.$#'
identifier: return.type
count: 1
path: src/mantle/support/class-collection.php

-
message: '#^Method Mantle\\Support\\Collection\:\:where_instance_of\(\) should return static\(Mantle\\Support\\Collection\<TKey of \(int\|string\), TWhereInstanceOf\>\) but returns static\(Mantle\\Support\\Collection\<TKey of \(int\|string\), TValue\>\)\.$#'
identifier: return.type
count: 1
path: src/mantle/support/class-collection.php
3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
includes:
- phpstan-baseline.neon

parameters:
# Level 9 is the highest level
level: 5
Expand Down
11 changes: 6 additions & 5 deletions src/mantle/database/model/class-model.php
Original file line number Diff line number Diff line change
Expand Up @@ -565,33 +565,34 @@ public static function all(): Collection {
* Create a new instance of a model and save it.
*
* @param array $args Model arguments.
* @return static
* @return static<TModelObject>
*/
public static function create( array $args ) {
public static function create( array $args ): static {
$instance = new static();

if ( $instance instanceof Updatable ) {
$instance->save( $args );
$instance->refresh();
}

return $instance;
return $instance; // @phpstan-ignore-line return.type
}

/**
* Get the first record matching the attributes or instantiate it.
*
* @param array $attributes Attributes to match.
* @param array $values Values to set.
* @return static<TModelObject>
*/
public static function first_or_new( array $attributes, array $values = [] ): static {
$instance = static::query()->where( $attributes )->first();

if ( ! $instance ) {
return new static( array_merge( $attributes, $values ) );
return new static( array_merge( $attributes, $values ) ); // @phpstan-ignore-line return.type
}

return $instance;
return $instance; // @phpstan-ignore-line return.type
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/mantle/database/query/class-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,10 @@ public function whereNotIn( string $attribute, array $values ) {
* Create a query builder for a model.
*
* @param array|string $model Model name or array of model names.
* @return static
* @return static<TModel>
*/
public static function create( $model ) {
return new static( $model );
return new static( $model ); // @phpstan-ignore-line return.type
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* Used to mark a test as expecting a deprecation notice.
*/
#[Attribute]
#[Attribute( Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE )]
class Expected_Deprecation {
/**
* Constructor.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* Used to mark a test as expecting a specific doing it wrong call. Supports * as a wildcard.
*/
#[Attribute]
#[Attribute( Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE )]
class Expected_Incorrect_Usage {
/**
* Constructor.
Expand Down
2 changes: 1 addition & 1 deletion src/mantle/testing/attributes/class-ignore-deprecation.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* Used to mark a test as ignoring a specific deprecation notice. Supports * as a wildcard.
*/
#[Attribute]
#[Attribute( Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE )]
class Ignore_Deprecation {
/**
* Constructor.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* Used to mark a test as ignoring a specific doing it wrong call. Supports * as a wildcard.
*/
#[Attribute]
#[Attribute( Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE )]
class Ignore_Incorrect_Usage {
/**
* Constructor.
Expand Down
Loading