Skip to content

Commit

Permalink
PHStan for tests & lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
swissspidy committed May 16, 2024
1 parent 23f659e commit 1830a06
Show file tree
Hide file tree
Showing 49 changed files with 742 additions and 425 deletions.
10 changes: 6 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,18 @@
"wearerequired/traduttore-registry": "^2.0"
},
"require-dev": {
"dealerdirect/phpcodesniffer-composer-installer": "^v1.0.0",
"php-stubs/wp-cli-stubs": "^v2.10",
"dealerdirect/phpcodesniffer-composer-installer": "^1.0.0",
"php-stubs/wordpress-tests-stubs": "^6.5",
"php-stubs/wp-cli-stubs": "^2.10",
"phpstan/extension-installer": "^1.3",
"phpstan/phpstan-deprecation-rules": "^1.2",
"phpstan/phpstan-phpunit": "^1.4",
"swissspidy/phpstan-no-private": "^0.2.0",
"szepeviktor/phpstan-wordpress": "^1.3",
"wearerequired/coding-standards": "^6.0",
"wp-cli/extension-command": "^2.0",
"wp-cli/rewrite-command": "^2.0",
"wp-cli/wp-cli-tests": "^v4.2.9",
"wp-cli/wp-cli-tests": "^4.2.9",
"wpackagist-plugin/glotpress": "^4.0.0",
"yoast/phpunit-polyfills": "^1.1"
},
Expand All @@ -75,8 +77,8 @@
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true,
"composer/installers": true,
"dealerdirect/phpcodesniffer-composer-installer": true,
"phpstan/extension-installer": true
},
"process-timeout": 7200,
Expand Down
2 changes: 1 addition & 1 deletion inc/CLI/InfoCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ protected function get_svn_binary_path(): ?string {
* @return null|string Binary path on success, null otherwise.
*/
protected function get_wp_binary_path(): ?string {
if ( \defined( 'TRADUTTORE_WP_BIN' ) && TRADUTTORE_WP_BIN ) {
if ( \defined( 'TRADUTTORE_WP_BIN' ) && \is_string( TRADUTTORE_WP_BIN ) ) {
return TRADUTTORE_WP_BIN;
}

Expand Down
4 changes: 2 additions & 2 deletions inc/CLI/ProjectCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ public function info( array $args, array $assoc_args ): void {
$repository_visibility = $project->get_repository_visibility() ?? '(unknown)';
$repository_ssh_url = $repository ? $repository->get_ssh_url() : '(unknown)';
$repository_https_url = $repository ? $repository->get_https_url() : '(unknown)';
$repository_instance = $repository ? \get_class( $repository ) : '(unknown)';
$loader_instance = $loader ? \get_class( $loader ) : '(unknown)';
$repository_instance = $repository ? $repository::class : '(unknown)';

Check failure on line 91 in inc/CLI/ProjectCommand.php

View workflow job for this annotation

GitHub Actions / Lint

PHP syntax error: Cannot use ::class with dynamic class name
$loader_instance = $loader ? $loader::class : '(unknown)';

if ( get_flag_value( $assoc_args, 'format' ) === 'json' ) {
$info = [
Expand Down
22 changes: 11 additions & 11 deletions inc/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ class Configuration {
*
* @var string Repository path.
*/
protected $path;
protected string $path;

/**
* Repository configuration.
*
* @since 3.0.0
*
* @phpstan-var ProjectConfig
*
* @var array Repository configuration.
*
* @phpstan-var ProjectConfig
*/
protected $config = [];

Expand Down Expand Up @@ -64,9 +64,9 @@ public function get_path(): string {
*
* @since 3.0.0
*
* @phpstan-return ProjectConfig
*
* @return array<string,string|string[]> Repository configuration.
*
* @phpstan-return ProjectConfig
*/
public function get_config(): array {
return $this->config;
Expand All @@ -77,14 +77,14 @@ public function get_config(): array {
*
* @since 3.0.0
*
* @param string $key Config key.
* @return string|string[]|null Config value.
*
* @phpstan-template T of key-of<ProjectConfig>
* @phpstan-param T $key
* @phpstan-return ProjectConfig[T] | null
*
* @param string $key Config key.
* @return string|string[]|null Config value.
*/
public function get_config_value( string $key ) {
public function get_config_value( string $key ): string|array|null {

Check failure on line 87 in inc/Configuration.php

View workflow job for this annotation

GitHub Actions / Lint

PHP syntax error: syntax error, unexpected '|', expecting ';' or '{'
if ( isset( $this->config[ $key ] ) ) {
return $this->config[ $key ];
}
Expand All @@ -97,9 +97,9 @@ public function get_config_value( string $key ) {
*
* @since 3.0.0
*
* @phpstan-return ProjectConfig
*
* @return array<string,string|string[]> Configuration data if found.
*
* @phpstan-return ProjectConfig
*/
protected function load_config(): array {
$config_file = trailingslashit( $this->path ) . 'traduttore.json';
Expand Down
16 changes: 8 additions & 8 deletions inc/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use GP;
use GP_Locales;
use GP_Translation_Set;
use InvalidArgumentException;

/**
* Export strings to translation files in PO, MO, and JSON format.
Expand All @@ -25,7 +24,7 @@ class Export {
*
* @var \GP_Translation_Set
*/
protected $translation_set;
protected GP_Translation_Set $translation_set;

/**
* The current locale.
Expand All @@ -43,7 +42,7 @@ class Export {
*
* @var \Required\Traduttore\Project
*/
protected $project;
protected Project $project;

/**
* List of generated files.
Expand All @@ -52,7 +51,7 @@ class Export {
*
* @var string[]
*/
protected $files;
protected array $files;

/**
* Export constructor.
Expand All @@ -63,12 +62,13 @@ public function __construct( GP_Translation_Set $translation_set ) {
$this->translation_set = $translation_set;
$this->locale = GP_Locales::by_slug( $translation_set->locale );

/**
* GlotPress project.
*
* @var \GP_Project $gp_project
*/
$gp_project = GP::$project->get( $translation_set->project_id );

if ( ! $gp_project ) {
throw new InvalidArgumentException( __( 'Project not found', 'traduttore' ) );
}

$this->project = new Project( $gp_project );
}

Expand Down
2 changes: 1 addition & 1 deletion inc/Loader/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ abstract class Base implements Loader {
*
* @var \Required\Traduttore\Repository Repository object.
*/
protected $repository;
protected Repository $repository;

/**
* Class constructor.
Expand Down
12 changes: 6 additions & 6 deletions inc/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ static function ( $events ) {
'description' => __( 'When a new translation ZIP file is built', 'traduttore' ),
'message' => function ( $zip_path, $zip_url, GP_Translation_Set $translation_set ) {
/** @var \GP_Locale $locale */
$locale = GP_Locales::by_slug( $translation_set->locale );
$locale = GP_Locales::by_slug( $translation_set->locale );

$gp_project = GP::$project->get( $translation_set->project_id );

Expand Down Expand Up @@ -397,10 +397,10 @@ public function filter_restricted_site_access_is_restricted( bool $is_restricted
*
* @since 3.0.0
*
* @phpstan-param \WP_REST_Request<array{}> $request
*
* @param \WP_REST_Request $request Request object.
* @return bool True if permission is granted, false otherwise.
*
* @phpstan-param \WP_REST_Request<array{}> $request
*/
public function incoming_webhook_permission_callback( WP_REST_Request $request ): bool {
$result = false;
Expand Down Expand Up @@ -429,12 +429,12 @@ public function incoming_webhook_permission_callback( WP_REST_Request $request )
*
* @since 3.0.0
*
* @phpstan-param \WP_REST_Request<array{}> $request
*
* @param \WP_REST_Request $request Request object.
* @return \WP_Error|\WP_REST_Response REST response on success, error object on failure.
*
* @phpstan-param \WP_REST_Request<array{}> $request
*/
public function incoming_webhook_callback( WP_REST_Request $request ) {
public function incoming_webhook_callback( WP_REST_Request $request ): \WP_Error|\WP_REST_Response {

Check failure on line 437 in inc/Plugin.php

View workflow job for this annotation

GitHub Actions / Lint

PHP syntax error: syntax error, unexpected '|', expecting ';' or '{'
$result = new \WP_Error( '400', 'Bad request' );
$handler = ( new WebhookHandlerFactory() )->get_handler( $request );

Expand Down
2 changes: 1 addition & 1 deletion inc/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class Project {
*
* @var \GP_Project Project information.
*/
protected $project;
protected GP_Project $project;

/**
* Project constructor.
Expand Down
10 changes: 5 additions & 5 deletions inc/ProjectLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ class ProjectLocator {
*
* @var \Required\Traduttore\Project|null Project instance.
*/
protected $project;
protected ?Project $project;

/**
* ProjectLocator constructor.
*
* @since 2.0.0
*
* @param int|string|Project|GP_Project $project Possible GlotPress project ID or path or source code repository path.
* @param int|string|false|null|\Required\Traduttore\Project|\GP_Project $project Possible GlotPress project ID or path or source code repository path.
*/
public function __construct( $project ) {
public function __construct( int|string|false|\Required\Traduttore\Project|\GP_Project|null $project ) {

Check failure on line 35 in inc/ProjectLocator.php

View workflow job for this annotation

GitHub Actions / Lint

PHP syntax error: syntax error, unexpected '|', expecting variable (T_VARIABLE)
$this->project = $this->find_project( $project );
}

Expand All @@ -52,10 +52,10 @@ public function get_project(): ?Project {
*
* @since 2.0.0
*
* @param int|string|Project|GP_Project $project Possible GlotPress project ID or path or source code repository path.
* @param int|string|false|null|\Required\Traduttore\Project|\GP_Project $project Possible GlotPress project ID or path or source code repository path.
* @return \Required\Traduttore\Project Project instance.
*/
protected function find_project( $project ): ?Project {
protected function find_project( int|string|false|\Required\Traduttore\Project|\GP_Project|null $project ): ?Project {
if ( ! $project ) {
return null;
}
Expand Down
4 changes: 2 additions & 2 deletions inc/Repository/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ abstract class Base implements Repository {
*
* @var \Required\Traduttore\Project Project information.
*/
protected $project;
protected Project $project;

/**
* Loader constructor.
Expand Down Expand Up @@ -106,7 +106,7 @@ public function get_name(): string {
}

if ( $url ) {
$path = wp_parse_url( $url, PHP_URL_PATH );
$path = wp_parse_url( $url, PHP_URL_PATH );
$path = $path ? trim( $path, '/' ) : '';
$parts = explode( '/', $path );
$name = implode( '/', array_splice( $parts, 0, 2 ) );
Expand Down
4 changes: 2 additions & 2 deletions inc/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Runner {
*
* @var \Required\Traduttore\Loader VCS loader.
*/
protected $loader;
protected Loader $loader;

/**
* Updater instance.
Expand All @@ -31,7 +31,7 @@ class Runner {
*
* @var \Required\Traduttore\Updater Translation updater.
*/
protected $updater;
protected Updater $updater;

/**
* Runner constructor.
Expand Down
4 changes: 2 additions & 2 deletions inc/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Updater {
*
* @var \Required\Traduttore\Project Project information.
*/
protected $project;
protected Project $project;

/**
* Returns a new loader instance for a given project.
Expand Down Expand Up @@ -221,7 +221,7 @@ protected function create_pot_file( Configuration $config ): ?string {
* @return string WP-CLI binary path.
*/
protected function get_wp_bin(): string {
if ( \defined( 'TRADUTTORE_WP_BIN' ) && TRADUTTORE_WP_BIN ) {
if ( \defined( 'TRADUTTORE_WP_BIN' ) && \is_string( TRADUTTORE_WP_BIN ) ) {
return TRADUTTORE_WP_BIN;
}

Expand Down
6 changes: 3 additions & 3 deletions inc/WebhookHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ interface WebhookHandler {
*
* @since 3.0.0
*
* @phpstan-param \WP_REST_Request<array{}> $request
*
* @param \WP_REST_Request $request Request object.
*
* @phpstan-param \WP_REST_Request<array{}> $request
*/
public function __construct( WP_REST_Request $request );

Expand All @@ -42,5 +42,5 @@ public function permission_callback(): ?bool;
*
* @return \WP_Error|\WP_REST_Response REST response on success, error object on failure.
*/
public function callback();
public function callback(): \WP_Error|\WP_REST_Response;

Check failure on line 45 in inc/WebhookHandler.php

View workflow job for this annotation

GitHub Actions / Lint

PHP syntax error: syntax error, unexpected '|', expecting ';' or '{'
}
8 changes: 4 additions & 4 deletions inc/WebhookHandler/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ abstract class Base implements WebhookHandler {
*
* @phpstan-var \WP_REST_Request<array{}>
*/
protected $request;
protected WP_REST_Request $request;

/**
* Class constructor.
*
* @since 3.0.0
*
* @phpstan-param \WP_REST_Request<array{}> $request
*
* @param \WP_REST_Request $request Request object.
*
* @phpstan-param \WP_REST_Request<array{}> $request
*/
public function __construct( WP_REST_Request $request ) {
$this->request = $request;
Expand Down Expand Up @@ -79,7 +79,7 @@ protected function get_secret( ?Project $project = null ): ?string {
*
* @since 3.0.0
*
* @param string $secret Webhook sync secret.
* @param string|null $secret Webhook sync secret.
* @param \Required\Traduttore\WebhookHandler $handler The current webhook handler instance.
* @param \Required\Traduttore\Project|null $project The current project if passed through.
*/
Expand Down
4 changes: 2 additions & 2 deletions inc/WebhookHandler/Bitbucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function permission_callback(): ?bool {
return false;
}

$token = $this->request->get_header( 'x-hub-signature' );
$token = $this->request->get_header( 'x-hub-signature-256' );
$params = $this->request->get_params();
$locator = new ProjectLocator( $params['repository']['links']['html']['href'] ?? null );
$project = $locator->get_project();
Expand All @@ -60,7 +60,7 @@ public function permission_callback(): ?bool {
*
* @return \WP_Error|\WP_REST_Response REST response on success, error object on failure.
*/
public function callback() {
public function callback(): \WP_Error|\WP_REST_Response {

Check failure on line 63 in inc/WebhookHandler/Bitbucket.php

View workflow job for this annotation

GitHub Actions / Lint

PHP syntax error: syntax error, unexpected '|', expecting ';' or '{'
$params = $this->request->get_params();

$locator = new ProjectLocator( $params['repository']['links']['html']['href'] );
Expand Down
Loading

0 comments on commit 1830a06

Please sign in to comment.