Skip to content

Commit

Permalink
PHPStan: Phase 2 (#385)
Browse files Browse the repository at this point in the history
* Adding phpstan to facade

* Adding phpstan to testing

* Continued work for phpstan

* PHPStan filesystem start

* Contd work

* Adjusting logger for phpstan

* Adjusting logger for phpstan

* PHPStan for filesystem

* Adding phpstan for console

* Adding phpstan to testkit

* Adding it to view

* PHPStan fixes
  • Loading branch information
srtfisher committed May 10, 2023
1 parent f606abe commit 05abd59
Show file tree
Hide file tree
Showing 52 changed files with 263 additions and 202 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
},
"require-dev": {
"alleyinteractive/alley-coding-standards": "^1.0",
"league/flysystem-aws-s3-v3": "^1.0",
"mockery/mockery": "^1.3",
"php-stubs/wp-cli-stubs": "^2.7",
"phpunit/phpunit": "^9.3.3",
Expand Down Expand Up @@ -115,7 +116,7 @@
"phpcbf": "phpcbf --standard=./phpcs.xml .",
"phpcs": "phpcs --standard=./phpcs.xml .",
"phpcs-modified": "./bin/phpcs-modified-files.sh",
"phpstan": "phpstan --memory-limit=512M",
"phpstan": "phpstan --memory-limit=1024M",
"phpunit": "phpunit",
"release": "monorepo-builder release --ansi",
"release:patch": "monorepo-builder release patch --ansi",
Expand Down
41 changes: 23 additions & 18 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,28 @@ parameters:
- src/mantle/application
- src/mantle/assets
- src/mantle/auth
# - src/mantle/blocks
- src/mantle/cache
- src/mantle/config
# - src/mantle/console
- 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/events
- src/mantle/facade
- src/mantle/faker
- 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/log
- src/mantle/new-relic
# - src/mantle/queue
# - src/mantle/rest-api
- src/mantle/rest-api
# - src/mantle/scheduling
# - src/mantle/support
# - src/mantle/testing
# - src/mantle/testkit
# - src/mantle/view
- src/mantle/testing
- src/mantle/testkit
- src/mantle/view
- mantle.php

ignoreErrors:
Expand All @@ -43,7 +40,15 @@ parameters:
scanFiles:
- %rootDir%/../../php-stubs/wp-cli-stubs/wp-cli-stubs.php

# excludePaths:
# - ./*/*/FileToBeExcluded.php
#
# checkMissingIterableValueType: false
scanDirectories:
- tests/fixtures/phpstan

excludePaths:
- src/mantle/blocks
- src/mantle/query-monitor
- src/mantle/testing/class-mock-action.php
- src/mantle/testing/doubles/class-mockphpmailer.php
- src/mantle/testing/install-wordpress.php
- src/mantle/testing/preload.php
- src/mantle/testing/wordpress-bootstrap.php
- src/mantle/testing/wp-tests-config-sample.php
8 changes: 4 additions & 4 deletions src/mantle/config/class-repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function all(): array {
* @param mixed $offset Offset to retrieve.
* @return bool
*/
public function offsetExists( $offset ): bool {
public function offsetExists( mixed $offset ): bool {
return $this->has( $offset );
}

Expand All @@ -93,7 +93,7 @@ public function offsetExists( $offset ): bool {
* @param mixed $offset Offset to retrieve.
* @return mixed
*/
public function offsetGet( $offset ) {
public function offsetGet( mixed $offset ): mixed {
return $this->get( $offset );
}

Expand All @@ -103,7 +103,7 @@ public function offsetGet( $offset ) {
* @param mixed $offset Offset to set.
* @param mixed $value Value to set.
*/
public function offsetSet( $offset, $value ) {
public function offsetSet( mixed $offset, mixed $value ): void {
$this->set( $offset, $value );
}

Expand All @@ -112,7 +112,7 @@ public function offsetSet( $offset, $value ) {
*
* @param mixed $offset Offset to unset.
*/
public function offsetUnset( $offset ) {
public function offsetUnset( mixed $offset ): void {
$this->set( $offset, null );
}
}
9 changes: 8 additions & 1 deletion src/mantle/console/class-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ abstract class Command extends Symfony_Command {
*/
protected $signature;

/**
* The command's help text.
*
* @var string
*/
protected string $help;

/**
* Container instance.
*
Expand All @@ -70,7 +77,7 @@ abstract class Command extends Symfony_Command {
public function __construct() {
// Infer the name from the signature.
if ( ! empty( $this->signature ) ) {
$this->set_definition_from_signature( $this->signature );
$this->set_definition_from_signature();
} else {
parent::__construct( $this->name );
}
Expand Down
18 changes: 10 additions & 8 deletions src/mantle/console/concerns/trait-interacts-with-io.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@
use Mantle\Contracts\Support\Arrayable;
use Mantle\Support\Str;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\Question;
use Mantle\Console\Output_Style;

Expand All @@ -31,9 +29,9 @@ trait Interacts_With_IO {
/**
* Output interface.
*
* @var OutputInterface|Output_Style
* @var Output_Style
*/
protected OutputInterface $output;
protected Output_Style $output;

/**
* The mapping between human readable verbosity levels and Symfony's OutputInterface.
Expand Down Expand Up @@ -444,18 +442,22 @@ public function set_input( InputInterface $input ) {
/**
* Retrieve the output interface.
*
* @return OutputInterface
* @return OutputInterface|Output_Style
*/
public function output(): OutputInterface {
public function output(): OutputInterface|Output_Style {
return $this->output;
}

/**
* Set the output implementation.
*
* @param OutputInterface $output Output interface.
* @param OutputInterface|Output_Style $output Output interface.
*/
public function set_output( OutputInterface $output ) {
public function set_output( OutputInterface|Output_Style $output ) {
if ( ! $output instanceof Output_Style ) {
$output = new Output_Style( $this->input, $output );
}

$this->output = $output;
}

Expand Down
2 changes: 1 addition & 1 deletion src/mantle/container/class-container.php
Original file line number Diff line number Diff line change
Expand Up @@ -1174,7 +1174,7 @@ public function offsetExists( mixed $key ): bool {
* @param mixed $key
* @return mixed
*/
public function offsetGet( mixed $key ) {
public function offsetGet( mixed $key ): mixed {
return $this->make( $key );
}

Expand Down
4 changes: 2 additions & 2 deletions src/mantle/contracts/events/interface-dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ interface Dispatcher {
/**
* Register an event listener with the dispatcher.
*
* @param string|array $events
* @param \Closure|string $listener
* @param string|array $events
* @param \Closure|callable $listener
* @return void
*/
public function listen( $events, $listener );
Expand Down
3 changes: 2 additions & 1 deletion src/mantle/contracts/interface-application.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ interface Application extends Container {
/**
* Getter for the base path.
*
* @param string $path Path to append.
* @return string
*/
public function get_base_path(): string;
public function get_base_path( string $path = '' ): string;

/**
* Set the base path for a application.
Expand Down
4 changes: 2 additions & 2 deletions src/mantle/events/class-dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function listen( $events, $listener, int $priority = 10 ) {
foreach ( (array) $events as $event ) {
add_action(
$event,
$this->make_listener( $listener, $event ),
$this->make_listener( $listener ),
$priority,
PHP_INT_MAX,
);
Expand Down Expand Up @@ -228,7 +228,7 @@ protected function parse_class_callable( $listener ) {
/**
* Remove a set of listeners from the dispatcher.
*
* @param string $event Event to remove.
* @param string|object $event Event to remove.
* @param callable|string $listener Listener to remove.
* @param int $priority Priority of the listener.
* @return void
Expand Down
14 changes: 10 additions & 4 deletions src/mantle/events/trait-wordpress-action.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
use ReflectionClass;
use ReflectionException;
use ReflectionFunction;
use ReflectionNamedType;
use ReflectionParameter;
use ReflectionUnionType;
use RuntimeException;

/**
Expand Down Expand Up @@ -131,7 +133,7 @@ protected function validate_argument_type( $argument, ReflectionParameter $param
return $argument;
}

if ( ! $type->isBuiltin() ) {
if ( $type instanceof ReflectionNamedType && ! $type->isBuiltin() ) {
$parameter_class = Reflector::get_parameter_class_name( $parameter );

if ( Reflector::is_parameter_subclass_of( $parameter, Enumerable::class ) ) {
Expand All @@ -157,7 +159,7 @@ protected function validate_argument_type( $argument, ReflectionParameter $param
* @param mixed $argument Argument value.
* @param ReflectionParameter $parameter Callback paramater.
*/
$modified_argument = $this->dispatch( 'mantle-typehint-resolve:' . $type->getName(), [ null, $argument, $parameter ], true );
$modified_argument = $this->dispatch( 'mantle-typehint-resolve:' . $type->getName(), [ null, $argument, $parameter ] );

if ( $modified_argument ) {
return $modified_argument;
Expand All @@ -167,14 +169,18 @@ protected function validate_argument_type( $argument, ReflectionParameter $param
}

// Ensure an 'Arrayable' interface is cast to an array properly.
if ( 'array' === $type->getName() && $argument instanceof Arrayable ) {
if ( $type instanceof ReflectionNamedType && 'array' === $type->getName() && $argument instanceof Arrayable ) {
return $argument->to_array();
}

// Handle type casting internal arguments.
$argument_type = gettype( $argument );

if ( $argument_type === $type->getName() ) {
if ( ! $type instanceof ReflectionNamedType ) {
throw new RuntimeException( $type::class . ' is not a supported type-hint.' );
}

if ( $type instanceof ReflectionNamedType && $argument_type === $type->getName() ) {
return $argument;
}

Expand Down
8 changes: 2 additions & 6 deletions src/mantle/facade/class-facade.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ abstract class Facade {
/**
* The application instance being facaded.
*
* @var \Mantle\Contracts\Application|\Mantle\Contracts\Container
* @var \Mantle\Contracts\Application|null
*/
protected static $app;
protected static ?Application $app = null;

/**
* The resolved object instances.
Expand Down Expand Up @@ -93,10 +93,6 @@ protected static function get_facade_root() {
* @return mixed
*/
protected static function resolve_facade_instance( $name ) {
if ( is_object( $name ) ) {
return $name;
}

if ( isset( static::$resolved_instances[ $name ] ) ) {
return static::$resolved_instances[ $name ];
}
Expand Down
2 changes: 1 addition & 1 deletion src/mantle/facade/class-queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Queue extends Facade {
/**
* Replace the bound instance with a fake.
*
* @return \Illuminate\Support\Testing\Fakes\QueueFake
* @return Queue_Fake
*/
public static function fake() {
$fake = new Queue_Fake( static::$app );
Expand Down
24 changes: 14 additions & 10 deletions src/mantle/filesystem/class-filesystem-adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use League\Flysystem\AwsS3v3\AwsS3Adapter;
use League\Flysystem\Cached\CachedAdapter;
use League\Flysystem\FileNotFoundException;
use League\Flysystem\FilesystemInterface;
use League\Flysystem\Filesystem as Flysystem;
use Mantle\Contracts\Filesystem\Filesystem;
use Mantle\Http\Uploaded_File;
use Mantle\Support\Arr;
Expand All @@ -35,16 +35,16 @@ class Filesystem_Adapter implements Filesystem {
/**
* Filesystem instance.
*
* @var FilesystemInterface|\League\Flysystem\Filesystem
* @var Flysystem
*/
protected $driver;
protected Flysystem $driver;

/**
* Constructor.
*
* @param FilesystemInterface $driver Filesystem instance.
* @param Flysystem $driver Filesystem instance.
*/
public function __construct( FilesystemInterface $driver ) {
public function __construct( Flysystem $driver ) {
$this->driver = $driver;
}

Expand Down Expand Up @@ -229,7 +229,11 @@ public function path( string $path ): string {
$adapter = $adapter->getAdapter();
}

return $adapter->getPathPrefix() . $path;
if ( method_exists( $adapter, 'getPathPrefix' ) ) {
return $adapter->getPathPrefix() . $path;
}

return $path;
}

/**
Expand Down Expand Up @@ -285,7 +289,7 @@ function () use ( $path ) {
*
* @param string $path File path.
* @param string|null $name File name.
* @param array|null $headers HTTP headers.
* @param array $headers HTTP headers.
* @return StreamedResponse
*/
public function download( $path, $name = null, array $headers = [] ): StreamedResponse {
Expand Down Expand Up @@ -325,9 +329,9 @@ public function mime_type( string $path ) {
/**
* Write the contents of a file.
*
* @param string $path File path.
* @param string|resource $contents File contents.
* @param array|string $options Options for the files or a string visibility.
* @param string $path File path.
* @param string|File|Uploaded_File|StreamInterface|resource $contents File contents.
* @param array|string $options Options for the files or a string visibility.
* @return bool
*/
public function put( string $path, $contents, $options = [] ): bool {
Expand Down
Loading

0 comments on commit 05abd59

Please sign in to comment.