Skip to content

Commit

Permalink
PHPStan for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
swissspidy committed May 16, 2024
1 parent 23f659e commit ee5822d
Show file tree
Hide file tree
Showing 43 changed files with 590 additions and 218 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 ) ) {

Check failure on line 152 in inc/CLI/InfoCommand.php

View workflow job for this annotation

GitHub Actions / Lint

Function is_string() should be referenced via a fully qualified name.
return TRADUTTORE_WP_BIN;
}

Expand Down
20 changes: 10 additions & 10 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,12 +77,12 @@ 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 ) {
if ( isset( $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
8 changes: 5 additions & 3 deletions inc/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Export {
*
* @var \GP_Translation_Set
*/
protected $translation_set;
protected GP_Translation_Set $translation_set;

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

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

/**
* Export constructor.
*
* @param \GP_Translation_Set $translation_set The translation set this export is for.
*
* @throws \InvalidArgumentException
*/
public function __construct( GP_Translation_Set $translation_set ) {
$this->translation_set = $translation_set;
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
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
6 changes: 3 additions & 3 deletions inc/ProjectLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ 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|\Required\Traduttore\Project|\GP_Project $project Possible GlotPress project ID or path or source code repository path.
*/
public function __construct( $project ) {
$this->project = $this->find_project( $project );
Expand All @@ -52,7 +52,7 @@ 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|\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 {
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
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
2 changes: 1 addition & 1 deletion 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 Down
4 changes: 2 additions & 2 deletions inc/WebhookHandler/GitHub.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,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' );

if ( ! $token ) {
return false;
Expand All @@ -54,7 +54,7 @@ public function permission_callback(): ?bool {
return false;

Check warning on line 54 in inc/WebhookHandler/GitHub.php

View check run for this annotation

Codecov / codecov/patch

inc/WebhookHandler/GitHub.php#L54

Added line #L54 was not covered by tests
}

$payload_signature = 'sha1=' . hash_hmac( 'sha1', $this->request->get_body(), $secret );
$payload_signature = 'sha256=' . hash_hmac( 'sha256', $this->request->get_body(), $secret );

return hash_equals( $token, $payload_signature );
}
Expand Down
3 changes: 2 additions & 1 deletion inc/WebhookHandlerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ class WebhookHandlerFactory {
*
* @since 3.0.0

Check failure on line 24 in inc/WebhookHandlerFactory.php

View workflow job for this annotation

GitHub Actions / Lint

There must be a single blank line after a tag group
*
* @phpstan-param \WP_REST_Request<array{}> $request
*
* @param \WP_REST_Request $request Request object.

Check failure on line 27 in inc/WebhookHandlerFactory.php

View workflow job for this annotation

GitHub Actions / Lint

Expected 1 line between annotations groups, found 2.
* @return \Required\Traduttore\WebhookHandler Webhook handler instance.
*
* @phpstan-param \WP_REST_Request<array{}> $request
*/
public function get_handler( WP_REST_Request $request ): ?WebhookHandler {
$handler = null;
Expand Down
7 changes: 4 additions & 3 deletions inc/ZipProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use DateTime;
use DateTimeZone;
use GP;
use GP_Locale;
use GP_Locales;
use GP_Translation_Set;
use InvalidArgumentException;
Expand Down Expand Up @@ -42,7 +43,7 @@ class ZipProvider {
*
* @var \GP_Translation_Set The translation set.
*/
protected $translation_set;
protected GP_Translation_Set $translation_set;

/**
* The current GlotPress locale.
Expand All @@ -51,7 +52,7 @@ class ZipProvider {
*
* @var \GP_Locale The locale.
*/
protected $locale;
protected GP_Locale $locale;

/**
* The current project.
Expand All @@ -60,7 +61,7 @@ class ZipProvider {
*
* @var \Required\Traduttore\Project The project.
*/
protected $project;
protected Project $project;

/**
* ZipProvider constructor.
Expand Down
5 changes: 4 additions & 1 deletion phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
<description>Coding Standard for Traduttore</description>

<file>.</file>
<exclude-pattern>/tests/*</exclude-pattern>
<exclude-pattern>/tests/behat/*</exclude-pattern>
<exclude-pattern>/tests/features/*</exclude-pattern>
<exclude-pattern>/tests/phpstan/*</exclude-pattern>
<exclude-pattern>/tests/phpunit/data/*</exclude-pattern>

<rule ref="Required"/>

Expand Down
4 changes: 3 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ parameters:
- vendor/wordpress-plugin/glotpress/gp-includes/routes/_main.php
scanFiles:
- vendor/php-stubs/wp-cli-stubs/wp-cli-stubs.php
- vendor/php-stubs/wordpress-tests-stubs/wordpress-tests-stubs.php
scanDirectories:
- tests/phpstan/stubs/
- vendor/wordpress-plugin/glotpress/gp-includes/
- vendor/wordpress-plugin/glotpress/locales/
paths:
- inc/
# - test/
- tests/phpunit
Loading

0 comments on commit ee5822d

Please sign in to comment.