Skip to content

Commit

Permalink
Merge pull request #382 from alleyinteractive/phpstan-2023
Browse files Browse the repository at this point in the history
PHPStan: Phase 1
  • Loading branch information
srtfisher committed May 9, 2023
2 parents fbc9346 + 9a0d00b commit f606abe
Show file tree
Hide file tree
Showing 60 changed files with 444 additions and 332 deletions.
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@
"require-dev": {
"alleyinteractive/alley-coding-standards": "^1.0",
"mockery/mockery": "^1.3",
"php-stubs/wp-cli-stubs": "^2.7",
"phpunit/phpunit": "^9.3.3",
"symplify/monorepo-builder": "^10.1"
"predis/predis": "^2.0.2",
"symplify/monorepo-builder": "^10.1",
"szepeviktor/phpstan-wordpress": "^1.2"
},
"replace": {
"mantle-framework/assets": "self.version",
Expand Down Expand Up @@ -112,11 +115,13 @@
"phpcbf": "phpcbf --standard=./phpcs.xml .",
"phpcs": "phpcs --standard=./phpcs.xml .",
"phpcs-modified": "./bin/phpcs-modified-files.sh",
"phpstan": "phpstan --memory-limit=512M",
"phpunit": "phpunit",
"release": "monorepo-builder release --ansi",
"release:patch": "monorepo-builder release patch --ansi",
"test": [
"@phpcs",
"@phpstan",
"@phpunit"
],
"validate-monorepo": "monorepo-builder validate --ansi"
Expand Down
1 change: 1 addition & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

<exclude name="PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection" />
<exclude name="PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.Changed" />
<exclude name="Squiz.Commenting.FunctionComment.IncorrectTypeHint" />
</rule>

<rule ref="Generic.Arrays.DisallowLongArraySyntax" />
Expand Down
49 changes: 49 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
includes:
- vendor/szepeviktor/phpstan-wordpress/extension.neon

parameters:
# Level 9 is the highest level
level: 5

paths:
- src/mantle/application
- src/mantle/assets
- src/mantle/auth
# - src/mantle/blocks
- src/mantle/cache
- src/mantle/config
# - src/mantle/console
- src/mantle/container
- src/mantle/contracts
# - src/mantle/database
# - src/mantle/events
# - src/mantle/facade
# - src/mantle/faker
# - src/mantle/featherkit
# - src/mantle/filesystem
- src/mantle/framework
# - src/mantle/http
- src/mantle/http-client
# - src/mantle/log
# - src/mantle/new-relic
# - src/mantle/query-monitor
# - src/mantle/queue
# - src/mantle/rest-api
# - src/mantle/scheduling
# - src/mantle/support
# - src/mantle/testing
# - src/mantle/testkit
# - src/mantle/view
- mantle.php

ignoreErrors:
- "#Unsafe usage of new static#"
- "#PHPDoc tag @param references unknown parameter#"

scanFiles:
- %rootDir%/../../php-stubs/wp-cli-stubs/wp-cli-stubs.php

# excludePaths:
# - ./*/*/FileToBeExcluded.php
#
# checkMissingIterableValueType: false
6 changes: 3 additions & 3 deletions src/mantle/application/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
/**
* Get the available container instance.
*
* @param string|null $abstract Component name.
* @param array $parameters Parameters to pass to the class.
* @return mixed|\Mantle\Application\Application
* @param string|null $abstract Abstract to resolve.
* @param array<mixed> $parameters Parameters.
* @return mixed|Application
*/
function app( string $abstract = null, array $parameters = [] ) {
if ( empty( $abstract ) ) {
Expand Down
8 changes: 4 additions & 4 deletions src/mantle/application/class-application.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,18 +345,18 @@ protected function register_base_bindings() {
static::set_instance( $this );

$this->instance( 'app', $this );
$this->instance( Container\Container::class, $this );
$this->instance( Container::class, $this );
$this->instance( Container_Contract::class, $this );
$this->instance( static::class, $this );

$this->singleton(
Package_Manifest::class,
fn( $app ) => new Package_Manifest( $this->get_base_path(), $this->get_cached_packages_path(), $app ),
fn( $app ) => new Package_Manifest( $this->get_base_path(), $this->get_cached_packages_path() ),
);

$this->singleton(
Model_Manifest::class,
fn ( $app ) => new Model_Manifest( $this->get_app_path(), $this->get_cached_models_path(), $app ),
fn ( $app ) => new Model_Manifest( $this->get_app_path(), $this->get_cached_models_path() ),
);
}

Expand Down Expand Up @@ -431,7 +431,7 @@ public function bootstrap_with( array $bootstrappers, Kernel_Contract $kernel )
*/
public function register_configured_providers() {
// Get providers from the application config.
$providers = collect( $this->config->get( 'app.providers', [] ) );
$providers = collect( $this->make( 'config' )->get( 'app.providers', [] ) );

// Include providers from the package manifest.
$providers->push( ...$this->make( Package_Manifest::class )->providers() );
Expand Down
10 changes: 5 additions & 5 deletions src/mantle/assets/class-asset-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,14 @@ public function blocks(): array {
* Retrieve the path to an asset when invoked.
*
* @param string $path Asset path.
* @param string $manifest_directory Manifest directory, optional.
* @param string $build_directory Build directory, optional.
* @return string
*/
public function __invoke( string $path, ?string $manifest_directory = null ): string {
if ( $manifest_directory ) {
return ( new static( $manifest_directory ) )->url( $path );
public function __invoke( string $path, ?string $build_directory = null ): string {
if ( $build_directory ) {
return ( new static( $build_directory ) )->url( $path );
}

return $this->url( $path, $manifest_directory );
return $this->url( $path );
}
}
18 changes: 9 additions & 9 deletions src/mantle/assets/class-asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ class Asset {
/**
* Constructor.
*
* @param string $type Asset type (script/style).
* @param string $handle Asset handle.
* @param string $src Script URL.
* @param string[]|string $deps Script dependencies.
* @param array|string $condition Condition to load.
* @param string $load_method Load method.
* @param string $load_hook Load hook.
* @param string|null $version Script version.
* @param bool $infer_from_loader Infer the asset from the loader if the source is not provided.
* @param string $type Asset type (script/style).
* @param string $handle Asset handle.
* @param string $src Asset URL.
* @param array<string> $deps Asset dependencies.
* @param array<string>|string $condition Condition to load.
* @param string $load_method Load method.
* @param string $load_hook Load hook.
* @param string|null $version Asset version.
* @param bool $infer_from_loader Infer the asset from the loader if the source is not provided.
*/
public function __construct(
protected string $type,
Expand Down
2 changes: 1 addition & 1 deletion src/mantle/cache/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*
* If an array is passed, we'll assume you want to put to the cache.
*
* @param dynamic key|key,default|data,expiration|null
* @param mixed $args Arguments.
* @return mixed|\Mantle\Framework\Cache\Cache_Manager
*
* @throws \Exception
Expand Down
12 changes: 7 additions & 5 deletions src/mantle/cache/class-array-repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ class Array_Repository extends Repository implements Repository_Contract {
/**
* Retrieve a value from cache.
*
* @param string $key Cache key.
* @param mixed $default Default value.
* @return mixed
* @template TCacheValue
*
* @param string $key Cache key.
* @param TCacheValue|(\Closure(): TCacheValue) $default Default value.
* @return (TCacheValue is null ? mixed : TCacheValue)
*/
public function get( string $key, mixed $default = null ): mixed {
if ( ! isset( $this->storage[ $key ] ) ) {
Expand Down Expand Up @@ -66,7 +68,7 @@ public function pull( $key, $default = null ) {
* @param \DateTimeInterface|\DateInterval|int|null $ttl
* @return bool
*/
public function put( $key, $value, $ttl = null ) {
public function put( $key, $value, $ttl = null ): bool {
$this->storage[ $key ] = [
'expire_at' => Carbon::now()->addSeconds( $ttl ?: 0 )->getTimestamp(),
'value' => $value,
Expand Down Expand Up @@ -105,7 +107,7 @@ public function decrement( $key, $value = 1 ) {
* @param string $key Cache key.
* @return bool
*/
public function forget( $key ) {
public function forget( $key ): bool {
unset( $this->storage[ $key ] );
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/mantle/cache/class-cache-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ protected function create_wordpress_driver( array $config ): Repository {
* @return Repository
*/
protected function create_array_driver( array $config ): Repository {
return new Array_Repository( $config['prefix'] ?? '' );
return new Array_Repository();
}

/**
Expand Down
18 changes: 9 additions & 9 deletions src/mantle/cache/class-redis-repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Redis_Repository extends Repository implements Taggable_Repository {
* @param Client|array $config Configuration options or client.
* @param string $prefix Prefix for caching.
*/
public function __construct( $config, string $prefix = '' ) {
public function __construct( array|Client $config, string $prefix = '' ) {
$this->set_connection( $config );

$this->prefix = $prefix;
Expand All @@ -45,7 +45,7 @@ public function __construct( $config, string $prefix = '' ) {
*
* @param Client|array $config Configuration instance.
*/
protected function set_connection( $config ) {
protected function set_connection( Client|array $config ) {
if ( $config instanceof Client ) {
return $config;
}
Expand Down Expand Up @@ -80,13 +80,13 @@ public function get( string $key, mixed $default = null ): mixed {
* @param \DateTimeInterface|\DateInterval|int|null $ttl
* @return bool
*/
public function put( $key, $value, $ttl = null ) {
public function put( $key, $value, $ttl = null ): bool {
$value = maybe_serialize( $value );

if ( is_null( $ttl ) ) {
return $this->client->set( $this->prefix . $key, $value );
return (bool) $this->client->set( $this->prefix . $key, $value );
} else {
return $this->client->setex( $this->prefix . $key, $ttl, $value );
return (bool) $this->client->setex( $this->prefix . $key, $ttl, $value );
}
}

Expand Down Expand Up @@ -118,15 +118,15 @@ public function decrement( $key, $value = 1 ) {
* @param string $key Cache key.
* @return bool
*/
public function forget( $key ) {
return $this->client->del( $key );
public function forget( $key ): bool {
return (bool) $this->client->del( $key );
}

/**
* Clear the cache.
*/
public function clear(): bool {
return $this->client->flushall();
return (bool) $this->client->flushall();
}

/**
Expand All @@ -137,7 +137,7 @@ public function clear(): bool {
*/
public function tags( $names ) {
if ( is_array( $names ) ) {
$names = sort( $names );
sort( $names );
$names = implode( $names );
} else {
$names = (string) $names;
Expand Down
37 changes: 34 additions & 3 deletions src/mantle/cache/class-repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,39 @@

/**
* Cache repository.
*
* This class contains some camelCase methods to match the PSR interface.
*/
abstract class Repository {
/**
* Retrieve a value from cache.
*
* @template TCacheValue
*
* @param string $key Cache key.
* @param TCacheValue|(\Closure(): TCacheValue) $default Default value.
* @return (TCacheValue is null ? mixed : TCacheValue)
*/
abstract public function get( string $key, mixed $default = null ): mixed;

/**
* Remove an item from the cache.
*
* @param string $key Cache key.
* @return bool
*/
abstract public function forget( $key ): bool;

/**
* Store an item in the cache.
*
* @param string $key
* @param mixed $value
* @param \DateTimeInterface|\DateInterval|int|null $ttl
* @return bool
*/
abstract public function put( $key, $value, $ttl = null ): bool;

/**
* Retrieve an item from the cache and delete it.
*
Expand Down Expand Up @@ -58,7 +89,7 @@ public function add( $key, $value, $ttl = null ) {
* @param string $key Cache key.
* @param mixed $value Item value.
* @param null|int|\DateInterval $ttl TTL.
* @return mixed
* @return bool
*/
public function set( string $key, mixed $value, null|int|\DateInterval $ttl = null ): bool {
return $this->put( $key, $value, $ttl );
Expand All @@ -68,7 +99,7 @@ public function set( string $key, mixed $value, null|int|\DateInterval $ttl = nu
* Delete a cache key.
*
* @param string $key Cache key.
* @return mixed
* @return bool
*/
public function delete( string $key ): bool {
return $this->forget( $key );
Expand Down Expand Up @@ -109,7 +140,7 @@ public function setMultiple( iterable $values, null|int|\DateInterval $ttl = nul
/**
* Delete multiple cache keys.
*
* @param string[]|string $keys Cache keys.
* @param iterable<string> $keys Cache keys.
* @return bool
*/
public function deleteMultiple( iterable $keys ): bool {
Expand Down
4 changes: 2 additions & 2 deletions src/mantle/cache/class-wordpress-repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function pull( $key, $default = null ) {
* @param \DateTimeInterface|\DateInterval|int|null $ttl
* @return bool
*/
public function put( $key, $value, $ttl = null ) {
public function put( $key, $value, $ttl = null ): bool {
return \wp_cache_set( $key, $value, $this->prefix, $ttl ); // phpcs:ignore WordPressVIPMinimum.Performance.LowExpiryCacheTime.CacheTimeUndetermined
}

Expand Down Expand Up @@ -98,7 +98,7 @@ public function decrement( $key, $value = 1 ) {
* @param string $key Cache key.
* @return bool
*/
public function forget( $key ) {
public function forget( $key ): bool {
return \wp_cache_delete( $key, $this->prefix );
}

Expand Down
6 changes: 2 additions & 4 deletions src/mantle/config/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

declare( strict_types=1 );

use Mantle\Application\Application;

if ( ! function_exists( 'config' ) ) {
/**
* Get a configuration value from the Configuration Repository.
Expand All @@ -24,9 +22,9 @@
*/
function config( string $key = null, $default = null ) {
if ( is_null( $key ) ) {
return Application::getInstance()->make( 'config' );
return app( 'config' );
}

return Application::getInstance()->make( 'config' )->get( $key, $default );
return app( 'config' )->get( $key, $default );
}
}
Loading

0 comments on commit f606abe

Please sign in to comment.