Skip to content

Commit

Permalink
PHPStan: Phase 3 (#386)
Browse files Browse the repository at this point in the history
* Starting pass at phpstan on database

* Contd work

* Continued work for database

* Starting work on collection

* Finishing collections

* PHPSTAN on interface

* Wrapping up for now

* Wrapping up phpstan for support
  • Loading branch information
srtfisher committed May 17, 2023
1 parent 05abd59 commit a7cd851
Show file tree
Hide file tree
Showing 46 changed files with 1,146 additions and 773 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
"phpcs-modified": "./bin/phpcs-modified-files.sh",
"phpstan": "phpstan --memory-limit=1024M",
"phpunit": "phpunit",
"phpunit:multisite": "WP_MULTISITE=1 phpunit",
"release": "monorepo-builder release --ansi",
"release:patch": "monorepo-builder release patch --ansi",
"test": [
Expand Down
46 changes: 46 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
parameters:
ignoreErrors:
-
message: "#^Method Mantle\\\\Support\\\\Collection\\:\\:map_to_groups\\(\\) should return Mantle\\\\Support\\\\Collection\\<TMapToGroupsKey of \\(int\\|string\\), Mantle\\\\Support\\\\Collection\\<int, TMapToGroupsValue\\>\\> but returns Mantle\\\\Support\\\\Collection\\<TMapToGroupsKey of \\(int\\|string\\), Mantle\\\\Support\\\\Collection\\<TMakeKey of \\(int\\|string\\), TMakeValue\\>\\>\\.$#"
count: 1
path: src/mantle/support/class-collection.php

-
message: "#^Parameter \\#1 \\$callback of method Mantle\\\\Support\\\\Collection\\<TKey of \\(int\\|string\\),TValue\\>\\:\\:when_empty\\(\\) expects callable\\(Mantle\\\\Support\\\\Collection\\<TKey of \\(int\\|string\\), TValue\\>\\)\\: TUnlessNotEmptyReturnType, callable\\(\\$this\\(Mantle\\\\Support\\\\Collection\\<TKey of \\(int\\|string\\), TValue\\>\\)\\)\\: TUnlessNotEmptyReturnType given\\.$#"
count: 1
path: src/mantle/support/class-collection.php

-
message: "#^Parameter \\#1 \\$callback of method Mantle\\\\Support\\\\Collection\\<TKey of \\(int\\|string\\),TValue\\>\\:\\:when_not_empty\\(\\) expects callable\\(Mantle\\\\Support\\\\Collection\\<TKey of \\(int\\|string\\), TValue\\>\\)\\: TUnlessEmptyReturnType, callable\\(\\$this\\(Mantle\\\\Support\\\\Collection\\<TKey of \\(int\\|string\\), TValue\\>\\)\\)\\: TUnlessEmptyReturnType given\\.$#"
count: 1
path: src/mantle/support/class-collection.php

-
message: "#^Parameter \\#2 \\$callback of method Mantle\\\\Support\\\\Collection\\<TKey of \\(int\\|string\\),TValue\\>\\:\\:when\\(\\) expects \\(callable\\(Mantle\\\\Support\\\\Collection\\<TKey of \\(int\\|string\\), TValue\\>, bool\\)\\: TWhenEmptyReturnType\\)\\|null, callable\\(\\$this\\(Mantle\\\\Support\\\\Collection\\<TKey of \\(int\\|string\\), TValue\\>\\)\\)\\: TWhenEmptyReturnType given\\.$#"
count: 1
path: src/mantle/support/class-collection.php

-
message: "#^Parameter \\#2 \\$callback of method Mantle\\\\Support\\\\Collection\\<TKey of \\(int\\|string\\),TValue\\>\\:\\:when\\(\\) expects \\(callable\\(Mantle\\\\Support\\\\Collection\\<TKey of \\(int\\|string\\), TValue\\>, bool\\)\\: TWhenNotEmptyReturnType\\)\\|null, callable\\(\\$this\\(Mantle\\\\Support\\\\Collection\\<TKey of \\(int\\|string\\), TValue\\>\\)\\)\\: TWhenNotEmptyReturnType given\\.$#"
count: 1
path: src/mantle/support/class-collection.php

-
message: "#^Parameter \\#2 \\$default of method Mantle\\\\Support\\\\Collection\\<TKey of \\(int\\|string\\),TValue\\>\\:\\:when_empty\\(\\) expects \\(callable\\(Mantle\\\\Support\\\\Collection\\<TKey of \\(int\\|string\\), TValue\\>\\)\\: TUnlessNotEmptyReturnType\\)\\|null, \\(callable\\(\\$this\\(Mantle\\\\Support\\\\Collection\\<TKey of \\(int\\|string\\), TValue\\>\\)\\)\\: TUnlessNotEmptyReturnType\\)\\|null given\\.$#"
count: 1
path: src/mantle/support/class-collection.php

-
message: "#^Parameter \\#2 \\$default of method Mantle\\\\Support\\\\Collection\\<TKey of \\(int\\|string\\),TValue\\>\\:\\:when_not_empty\\(\\) expects \\(callable\\(Mantle\\\\Support\\\\Collection\\<TKey of \\(int\\|string\\), TValue\\>\\)\\: TUnlessEmptyReturnType\\)\\|null, \\(callable\\(\\$this\\(Mantle\\\\Support\\\\Collection\\<TKey of \\(int\\|string\\), TValue\\>\\)\\)\\: TUnlessEmptyReturnType\\)\\|null given\\.$#"
count: 1
path: src/mantle/support/class-collection.php

-
message: "#^Parameter \\#3 \\$default of method Mantle\\\\Support\\\\Collection\\<TKey of \\(int\\|string\\),TValue\\>\\:\\:when\\(\\) expects \\(callable\\(Mantle\\\\Support\\\\Collection\\<TKey of \\(int\\|string\\), TValue\\>, bool\\)\\: TWhenEmptyReturnType\\)\\|null, \\(callable\\(\\$this\\(Mantle\\\\Support\\\\Collection\\<TKey of \\(int\\|string\\), TValue\\>\\)\\)\\: TWhenEmptyReturnType\\)\\|null given\\.$#"
count: 1
path: src/mantle/support/class-collection.php

-
message: "#^Parameter \\#3 \\$default of method Mantle\\\\Support\\\\Collection\\<TKey of \\(int\\|string\\),TValue\\>\\:\\:when\\(\\) expects \\(callable\\(Mantle\\\\Support\\\\Collection\\<TKey of \\(int\\|string\\), TValue\\>, bool\\)\\: TWhenNotEmptyReturnType\\)\\|null, \\(callable\\(\\$this\\(Mantle\\\\Support\\\\Collection\\<TKey of \\(int\\|string\\), TValue\\>\\)\\)\\: TWhenNotEmptyReturnType\\)\\|null given\\.$#"
count: 1
path: src/mantle/support/class-collection.php
9 changes: 6 additions & 3 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
includes:
- vendor/szepeviktor/phpstan-wordpress/extension.neon
- phpstan-baseline.neon
- vendor/szepeviktor/phpstan-wordpress/extension.neon

parameters:
# Level 9 is the highest level
Expand All @@ -14,7 +15,7 @@ parameters:
- src/mantle/console
- src/mantle/container
- src/mantle/contracts
# - src/mantle/database
- src/mantle/database
- src/mantle/events
- src/mantle/facade
- src/mantle/faker
Expand All @@ -27,7 +28,7 @@ parameters:
# - src/mantle/queue
- src/mantle/rest-api
# - src/mantle/scheduling
# - src/mantle/support
- src/mantle/support
- src/mantle/testing
- src/mantle/testkit
- src/mantle/view
Expand All @@ -36,6 +37,7 @@ parameters:
ignoreErrors:
- "#Unsafe usage of new static#"
- "#PHPDoc tag @param references unknown parameter#"
- "#@return is not subtype of template type TKey#"

scanFiles:
- %rootDir%/../../php-stubs/wp-cli-stubs/wp-cli-stubs.php
Expand All @@ -44,6 +46,7 @@ parameters:
- tests/fixtures/phpstan

excludePaths:
- src/mantle/database/factory
- src/mantle/blocks
- src/mantle/query-monitor
- src/mantle/testing/class-mock-action.php
Expand Down
84 changes: 84 additions & 0 deletions src/mantle/contracts/database/interface-model-meta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/**
* Model_Meta interface file.
*
* @package Mantle
*/

namespace Mantle\Contracts\Database;

use Mantle\Database\Model\Model_Exception;

/**
* Model Meta Interface
*/
interface Model_Meta {
/**
* Retrieve meta data for the object.
*
* @param string $meta_key Meta key to retrieve.
* @param bool $single Return the first meta key, defaults to true.
* @return mixed
*/
public function get_meta( string $meta_key, bool $single = true ): mixed;

/**
* Add meta value for the object.
*
* @param string $meta_key Meta key.
* @param mixed $meta_value Meta value to store.
* @param string $prev_value Optional, previous meta value.
*/
public function add_meta( string $meta_key, mixed $meta_value, mixed $prev_value = '' ): void;

/**
* Update meta value for the object.
*
* @param string $meta_key Meta key.
* @param mixed $meta_value Meta value to store.
* @param string $prev_value Optional, previous meta value.
*/
public function set_meta( string $meta_key, mixed $meta_value, mixed $prev_value = '' ): void;

/**
* Delete a object's meta.
*
* @param string $meta_key Meta key to delete by.
* @param mixed $meta_value Previous meta value to delete.
*/
public function delete_meta( string $meta_key, mixed $meta_value = '' );

/**
* Allow setting meta through an array via an attribute mutator.
*
* @param array $meta_values Meta values to set.
* @throws Model_Exception Thrown on invalid value being set.
*/
public function set_meta_attribute( array $meta_values ): void;

/**
* Get a queued meta attribute.
*
* @param string $key Meta key.
* @return mixed|null Meta value or null.
*/
public function get_queued_meta_attribute( string $key ): mixed;

/**
* Queue a meta attribute for saving.
* Allows meta to be set before a model is saved.
*
* Should not be called directly, only to be used via `$model->meta->...`.
*
* @param string $key Meta key.
* @param mixed $value Meta value.
* @param bool $update Flag to update the queued meta.
* @return void
*/
public function queue_meta_attribute( string $key, $value, bool $update = true ): void;

/**
* Store queued model meta.
*/
public function store_queued_meta(): void;
}
12 changes: 12 additions & 0 deletions src/mantle/contracts/filesystem/interface-filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,16 @@ public function set_visibility( string $path, string $visibility ): bool;
* @return string|null
*/
public function url( string $path ): ?string;

/**
* Get a temporary URL for the file at the given path.
*
* @param string $path File path.
* @param \DateTimeInterface $expiration File expiration.
* @param array $options Options for the URL.
* @return string
*
* @throws \RuntimeException Thrown on missing temporary URL.
*/
public function temporary_url( string $path, $expiration, array $options = [] ): string;
}
8 changes: 7 additions & 1 deletion src/mantle/contracts/support/interface-arrayable.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@

namespace Mantle\Contracts\Support;

/**
* Arrayable interface.
*
* @template TKey of array-key
* @template TValue
*/
interface Arrayable {
/**
* Get the instance as an array.
*
* @return array
* @return array<TKey, TValue>
*/
public function to_array();
}
2 changes: 0 additions & 2 deletions src/mantle/database/class-model-service-provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ public function register() {

/**
* Bootstrap the service provider.
*
* @throws Provider_Exception Thrown on invalid model.
*/
public function boot() {
parent::boot();
Expand Down
4 changes: 2 additions & 2 deletions src/mantle/database/class-seeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ abstract class Seeder {
/**
* The container instance.
*
* @var Container
* @var Container|null
*/
protected $container;

/**
* The console command instance.
*
* @var Command
* @var Command|null
*/
protected $command;

Expand Down
2 changes: 1 addition & 1 deletion src/mantle/database/console/class-seed-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Seed_Command extends Command {
/**
* Command signature.
*
* @var string|array
* @var string
*/
protected $signature = 'db:seed {--class=}';

Expand Down
2 changes: 1 addition & 1 deletion src/mantle/database/factory/class-factory-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ function () use ( $attributes ) {
$this->get_raw_attributes( $attributes )
);

if ( isset( $this->connection ) ) {
if ( empty( $this->connection ) ) {
$instance->setConnection( $this->connection );
}

Expand Down
21 changes: 12 additions & 9 deletions src/mantle/database/model/class-attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
/**
* Attachment Model
*/
class Attachment extends Post implements Contracts\Database\Core_Object, Contracts\Database\Updatable {
class Attachment extends Post {
/**
* Attachment type for the model.
*
Expand Down Expand Up @@ -41,8 +41,7 @@ public function image_url( $size ): ?string {
throw new Model_Exception( 'Unable to get attachment URL for unsaved attachment.' );
}

$url = wp_get_attachment_image_url( $this->id(), $size );
return $url ?? null;
return wp_get_attachment_image_url( $this->id(), $size ) ?: null;
}

/**
Expand All @@ -54,7 +53,9 @@ public function url(): ?string {
$settings = $this->get_cloud_settings();

if ( empty( $settings['disk'] ) ) {
return \wp_get_attachment_url( $this->id() ) ?? null;
$url = \wp_get_attachment_url( $this->id() );

return $url ?: null;
}

// For private attachments serve a temporary URL.
Expand All @@ -70,12 +71,13 @@ public function url(): ?string {
* Retrieve a temporary URL for a file.
*
* @param \DateTimeInterface $expiration File expiration.
* @return string
* @return string|null
*/
public function get_temporary_url( $expiration = null ): string {
public function get_temporary_url( $expiration = null ): ?string {
$settings = $this->get_cloud_settings();

if ( empty( $settings['disk'] ) ) {
return $settings;
return null;
}

$disk = $settings['disk'];
Expand Down Expand Up @@ -183,15 +185,16 @@ public function save( array $attributes = [] ) {
$id = $this->id();

if ( empty( $id ) ) {
$save = \wp_insert_attachment( $this->get_attributes() );
$save = \wp_insert_attachment( $this->get_attributes(), false, 0, true );
} else {
$save = \wp_update_post(
array_merge(
$this->get_modified_attributes(),
[
'ID' => $id,
]
)
),
true,
);
}

Expand Down
8 changes: 4 additions & 4 deletions src/mantle/database/model/class-comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
/**
* Comment Model
*/
class Comment extends Model implements Contracts\Database\Core_Object, Contracts\Database\Updatable {
class Comment extends Model implements Contracts\Database\Core_Object, Contracts\Database\Model_Meta, Contracts\Database\Updatable {
use Meta\Model_Meta,
Meta\Comment_Meta;

/**
* Attributes for the model from the object
*
* @var array
* @var array<string, string>
*/
protected static $aliases = [
'description' => 'comment_content',
Expand Down Expand Up @@ -166,8 +166,8 @@ public function save( array $attributes = [] ) {
);
}

if ( \is_wp_error( $save ) ) {
throw new Model_Exception( 'Error saving model: ' . $save->get_error_message() );
if ( ! $save ) {
throw new Model_Exception( 'Error saving model' );
}

$this->set_raw_attribute( 'comment_ID', $save );
Expand Down
Loading

0 comments on commit a7cd851

Please sign in to comment.