Skip to content

Commit

Permalink
Fix requests, PHPStan level 9
Browse files Browse the repository at this point in the history
  • Loading branch information
swissspidy committed May 16, 2024
1 parent 303513a commit 58f293a
Show file tree
Hide file tree
Showing 11 changed files with 266 additions and 158 deletions.
4 changes: 2 additions & 2 deletions inc/CLI/LanguagePackCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ public function list( array $args, array $assoc_args ): void {
* @param string[] $assoc_args Associative args.
*/
public function build( array $args, array $assoc_args ): void {
$all = get_flag_value( $assoc_args, 'all', false );
$force = get_flag_value( $assoc_args, 'force', false );
$all = (bool) get_flag_value( $assoc_args, 'all', false );
$force = (bool) get_flag_value( $assoc_args, 'force', false );
$projects = $this->check_optional_args_and_all( $args, $all );

if ( ! $projects ) {
Expand Down
4 changes: 2 additions & 2 deletions inc/CLI/ProjectCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ public function info( array $args, array $assoc_args ): void {
* @param string[] $assoc_args Associative args.
*/
public function update( array $args, array $assoc_args ): void {
$delete = get_flag_value( $assoc_args, 'delete', false );
$cached = get_flag_value( $assoc_args, 'cached', false );
$delete = (bool) get_flag_value( $assoc_args, 'delete', false );
$cached = (bool) get_flag_value( $assoc_args, 'cached', false );
$locator = new ProjectLocator( $args[0] );
$project = $locator->get_project();

Expand Down
32 changes: 25 additions & 7 deletions inc/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* @since 3.0.0
*
* @phpstan-type ProjectConfig array{ mergeWith?: string, textDomain?: string,exclude?: string }
* @phpstan-type ProjectConfig array{ mergeWith?: string, textDomain?: string,exclude?: string[] }
*/
class Configuration {
/**
Expand All @@ -29,9 +29,9 @@ class Configuration {
*
* @since 3.0.0
*
* @var array Repository configuration.
*
* @phpstan-var ProjectConfig
*
* @var array Repository configuration.
*/
protected $config = [];

Expand Down Expand Up @@ -64,7 +64,9 @@ public function get_path(): string {
*
* @since 3.0.0
*
* @return array<string,mixed> Repository configuration.
* @phpstan-return ProjectConfig

Check failure on line 67 in inc/Configuration.php

View workflow job for this annotation

GitHub Actions / Lint

Incorrect order of annotations groups.
*
* @return array<string,string|string[]> Repository configuration.
*/
public function get_config(): array {
return $this->config;
Expand All @@ -75,8 +77,12 @@ public function get_config(): array {
*
* @since 3.0.0
*
* @phpstan-template T of key-of<ProjectConfig>

Check failure on line 80 in inc/Configuration.php

View workflow job for this annotation

GitHub Actions / Lint

Incorrect order of annotations groups.
* @phpstan-param T $key
* @phpstan-return ProjectConfig[T] | null
*
* @param string $key Config key.
* @return mixed|null Config value.
* @return string|string[]|null Config value.
*/
public function get_config_value( string $key ) {
if ( isset( $this->config[ $key ] ) ) {
Expand All @@ -91,15 +97,21 @@ public function get_config_value( string $key ) {
*
* @since 3.0.0
*
* @return array<string,array<string,string>> Configuration data if found.
*
* @phpstan-return ProjectConfig

Check failure on line 100 in inc/Configuration.php

View workflow job for this annotation

GitHub Actions / Lint

Incorrect order of annotations groups.
*
* @return array<string,string|string[]> Configuration data if found.
*/
protected function load_config(): array {
$config_file = trailingslashit( $this->path ) . 'traduttore.json';
$composer_file = trailingslashit( $this->path ) . 'composer.json';

if ( file_exists( $config_file ) ) {
/**
* Traduttore configuration.
*
* @phpstan-var ProjectConfig $config
* @var array<string, string | string[]> $config
*/
$config = json_decode( (string) file_get_contents( $config_file ), true );

if ( $config ) {
Expand All @@ -108,6 +120,12 @@ protected function load_config(): array {
}

if ( file_exists( $composer_file ) ) {
/**
* Composer configuration.
*
* @phpstan-var array{extra?: array{ traduttore?: ProjectConfig } } $config
* @var array{extra?: array{ traduttore?: array<string, string | string[]> } } $config
*/
$config = json_decode( (string) file_get_contents( $composer_file ), true );

if ( $config && isset( $config['extra']['traduttore'] ) ) {
Expand Down
4 changes: 2 additions & 2 deletions inc/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use GP;
use GP_Locales;
use GP_Translation_Set;
use http\Exception\InvalidArgumentException;
use InvalidArgumentException;

/**
* Export strings to translation files in PO, MO, and JSON format.
Expand Down Expand Up @@ -228,7 +228,7 @@ protected function build_json_files( array $mapping ): void {
$contents = $format->print_exported_file( $this->project->get_project(), $this->locale, $this->translation_set, $entries );

// Add comment with file reference for debugging.
$contents_decoded = json_decode( $contents );
$contents_decoded = (object) json_decode( $contents );
$contents_decoded->comment = [ 'reference' => $file ];
$contents = (string) wp_json_encode( $contents_decoded );

Expand Down
55 changes: 55 additions & 0 deletions inc/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ public function get_source_url_template(): ?string {
* @return string|null Repository type if stored, null otherwise.
*/
public function get_repository_type(): ?string {
/**
* Project type.
*
* @var string|false $type
*/
$type = gp_get_meta( 'project', $this->project->id, static::REPOSITORY_TYPE_KEY );

return $type ?: null;
Expand All @@ -215,6 +220,11 @@ public function set_repository_type( string $type ): bool {
* @return null|string VCS type if stored, null otherwise.
*/
public function get_repository_vcs_type(): ?string {
/**
* VCS type.
*
* @var string|false $type
*/
$type = gp_get_meta( 'project', $this->project->id, static::REPOSITORY_VCS_TYPE_KEY );

return $type ?: null;
Expand All @@ -240,6 +250,11 @@ public function set_repository_vcs_type( string $type ): bool {
* @return null|string Repository URL if stored, null otherwise.
*/
public function get_repository_url(): ?string {
/**
* Repository URL.
*
* @var string|false $url
*/
$url = gp_get_meta( 'project', $this->project->id, static::REPOSITORY_URL_KEY );

return $url ?: null;
Expand All @@ -265,6 +280,11 @@ public function set_repository_url( string $url ): bool {
* @return null|string Repository name if stored, null otherwise.
*/
public function get_repository_name(): ?string {
/**
* Repository name.
*
* @var string|false $name
*/
$name = gp_get_meta( 'project', $this->project->id, static::REPOSITORY_NAME_KEY );

return $name ?: null;
Expand All @@ -290,6 +310,11 @@ public function set_repository_name( string $name ): bool {
* @return null|string Repository visibility if stored, null otherwise.
*/
public function get_repository_visibility(): ?string {
/**
* Repository visibility.
*
* @var string|false $visibility
*/
$visibility = gp_get_meta( 'project', $this->project->id, static::REPOSITORY_VISIBILITY_KEY );

return $visibility ?: null;
Expand All @@ -313,6 +338,11 @@ public function set_repository_visibility( string $visibility ): bool {
* @return null|string Repository SSH URL if stored, null otherwise.
*/
public function get_repository_ssh_url(): ?string {
/**
* Repository SSH URL.
*
* @var string|false $url
*/
$url = gp_get_meta( 'project', $this->project->id, static::REPOSITORY_SSH_URL_KEY );

return $url ?: null;
Expand All @@ -338,6 +368,11 @@ public function set_repository_ssh_url( string $url ): bool {
* @return null|string Repository HTTPS URL if stored, null otherwise.
*/
public function get_repository_https_url(): ?string {
/**
* Repository HTTPS URL.
*
* @var string|false $url
*/
$url = gp_get_meta( 'project', $this->project->id, static::REPOSITORY_HTTPS_URL_KEY );

return $url ?: null;
Expand All @@ -363,6 +398,11 @@ public function set_repository_https_url( string $url ): bool {
* @return null|string Webhook sync secret if stored, null otherwise.
*/
public function get_repository_webhook_secret(): ?string {
/**
* Repository webhook secret.
*
* @var string|false $name
*/
$name = gp_get_meta( 'project', $this->project->id, static::REPOSITORY_WEBHOOK_SECRET_KEY );

return $name ?: null;
Expand All @@ -388,6 +428,11 @@ public function set_repository_webhook_secret( string $secret ): bool {
* @return null|string Text domain if stored, null otherwise.
*/
public function get_text_domain(): ?string {
/**
* Project text domain
*
* @var string|false $name
*/
$name = gp_get_meta( 'project', $this->project->id, static::TEXT_DOMAIN_KEY );

return $name ?: null;
Expand All @@ -413,6 +458,11 @@ public function set_text_domain( string $name ): bool {
* @return null|\DateTime Last updated time if stored, null otherwise.
*/
public function get_last_updated_time(): ?DateTime {
/**
* Project last updated time.
*
* @var string|false $time
*/
$time = gp_get_meta( 'project', $this->project->id, static::UPDATE_TIME_KEY );

return $time ? new DateTime( $time, new DateTimeZone( 'UTC' ) ) : null;
Expand All @@ -438,6 +488,11 @@ public function set_last_updated_time( DateTime $time ): bool {
* @return null|string Version number if stored, null otherwise.
*/
public function get_version(): ?string {
/**
* Project version number.
*
* @var string|false $version
*/
$version = gp_get_meta( 'project', $this->project->id, static::VERSION_KEY );

return $version ?: null;
Expand Down
21 changes: 7 additions & 14 deletions inc/ProjectLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

use GP;
use GP_Project;
use http\Exception\InvalidArgumentException;

/**
* Helper class to find a GlotPress project based on path, ID, or GitHub repository URL.
Expand All @@ -22,7 +21,7 @@ class ProjectLocator {
*
* @since 2.0.0
*
* @var \Required\Traduttore\Project Project instance.
* @var \Required\Traduttore\Project|null Project instance.
*/
protected $project;

Expand All @@ -31,16 +30,10 @@ class ProjectLocator {
*
* @since 2.0.0
*
* @param mixed $project Possible GlotPress project ID or path or source code repository path.
* @param int|string|Project|GP_Project $project Possible GlotPress project ID or path or source code repository path.

Check failure on line 33 in inc/ProjectLocator.php

View workflow job for this annotation

GitHub Actions / Lint

Class name \Required\Traduttore\Project in @param should be referenced via a fully qualified name.

Check failure on line 33 in inc/ProjectLocator.php

View workflow job for this annotation

GitHub Actions / Lint

Class name \GP_Project in @param should be referenced via a fully qualified name.
*/
public function __construct( $project ) {
$found = $this->find_project( $project );

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

$this->project = $found;
$this->project = $this->find_project( $project );
}

/**
Expand All @@ -59,7 +52,7 @@ public function get_project(): ?Project {
*
* @since 2.0.0
*
* @param mixed $project Possible GlotPress project ID or path or source code repository path.
* @param int|string|Project|GP_Project $project Possible GlotPress project ID or path or source code repository path.

Check failure on line 55 in inc/ProjectLocator.php

View workflow job for this annotation

GitHub Actions / Lint

Class name \Required\Traduttore\Project in @param should be referenced via a fully qualified name.

Check failure on line 55 in inc/ProjectLocator.php

View workflow job for this annotation

GitHub Actions / Lint

Class name \GP_Project in @param should be referenced via a fully qualified name.
* @return \Required\Traduttore\Project Project instance.
*/
protected function find_project( $project ): ?Project {
Expand All @@ -82,15 +75,15 @@ protected function find_project( $project ): ?Project {
}

if ( ! $found ) {
$found = $this->find_by_repository_name( $project );
$found = $this->find_by_repository_name( (string) $project );
}

if ( ! $found ) {
$found = $this->find_by_repository_url( $project );
$found = $this->find_by_repository_url( (string) $project );
}

if ( ! $found ) {
$found = $this->find_by_source_url_template( $project );
$found = $this->find_by_source_url_template( (string) $project );
}

return $found ? new Project( $found ) : null;
Expand Down
9 changes: 8 additions & 1 deletion inc/WebhookHandler/GitHub.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', (string) wp_json_encode( $params ), $secret );
$payload_signature = 'sha1=' . hash_hmac( 'sha1', $this->request->get_body(), $secret );

return hash_equals( $token, $payload_signature );
}
Expand All @@ -75,11 +75,18 @@ public function callback() {

$params = $this->request->get_params();
$content_type = $this->request->get_content_type();

// See https://developer.github.com/webhooks/creating/#content-type.
if ( ! empty( $content_type ) && 'application/x-www-form-urlencoded' === $content_type['value'] ) {
$params = json_decode( $params['payload'], true );
}

/**
* Request params.
*
* @var array{repository: array{ default_branch?: string, html_url: string, full_name: string, ssh_url: string, clone_url: string, private: bool }, ref: string } $params
*/

if ( ! isset( $params['repository']['default_branch'] ) ) {
return new \WP_Error( '400', 'Request incomplete', [ 'status' => 400 ] );
}
Expand Down
7 changes: 6 additions & 1 deletion inc/ZipProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use GP;
use GP_Locales;
use GP_Translation_Set;
use http\Exception\InvalidArgumentException;
use InvalidArgumentException;
use ZipArchive;

/**
Expand Down Expand Up @@ -265,6 +265,11 @@ protected function get_zip_filename(): string {
* @return \DateTime Last build time.
*/
public function get_last_build_time(): ?DateTime {
/**
* Build time.
*
* @var string|false $meta
*/
$meta = gp_get_meta( 'translation_set', $this->translation_set->id, static::BUILD_TIME_KEY );

return $meta ? new DateTime( $meta, new DateTimeZone( 'UTC' ) ) : null;
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
parameters:
level: 8
level: 9
inferPrivatePropertyTypeFromConstructor: true
bootstrapFiles:
- tests/phpstan/bootstrap.php
Expand Down
Loading

0 comments on commit 58f293a

Please sign in to comment.