Skip to content

Commit

Permalink
switch from psalm to phpstan.
Browse files Browse the repository at this point in the history
switch to syde/phpcs.
  • Loading branch information
Chrico committed Jan 14, 2025
1 parent c79bb36 commit ff304a2
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 79 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/php-qa.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ on:
description: 'Choose jobs to run'
options:
- 'Run all'
- 'Run PHPCS only'
- 'Run PHPStan only'
- 'Run Psalm only'
- 'Run lint only'

Expand All @@ -50,11 +50,10 @@ jobs:
PHP_VERSION: '8.3'

static-code-analysis:
if: ${{ (github.event_name != 'workflow_dispatch') || ((github.event.inputs.jobs == 'Run all') || (github.event.inputs.jobs == 'Run Psalm only')) }}
if: ${{ (github.event_name != 'workflow_dispatch') || ((github.event.inputs.jobs == 'Run all') || (github.event.inputs.jobs == 'Run PHPStan only')) }}
uses: inpsyde/reusable-workflows/.github/workflows/static-analysis-php.yml@main
strategy:
matrix:
php: [ '7.4', '8.0', '8.1', '8.2', '8.3' ]
with:
PHP_VERSION: ${{ matrix.php }}
PSALM_ARGS: --output-format=github --no-suggestions --no-cache --no-diff --find-unused-psalm-suppress
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
},
"require-dev": {
"brain/monkey": "^2.6.1",
"inpsyde/php-coding-standards": "^2@dev",
"inpsyde/wp-stubs-versions": "dev-latest",
"syde/phpcs": "1.0.0-rc.1",
"roots/wordpress-no-content": "@dev",
"mikey179/vfsstream": "^v1.6.11",
"phpunit/phpunit": "^9.6.19",
"vimeo/psalm": "^5.24.0"
"phpstan/phpstan": "2.1.1",
"szepeviktor/phpstan-wordpress": "^2"
},
"extra": {
"branch-alias": {
Expand Down
49 changes: 13 additions & 36 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -1,46 +1,23 @@
<?xml version="1.0"?>
<ruleset>

<file>./src</file>
<file>./tests</file>

<arg value="sp"/>
<arg name="colors"/>
<config name="testVersion" value="7.4-"/>
<config name="ignore_warnings_on_exit" value="1"/>

<rule ref="Inpsyde">
<exclude name="WordPress.PHP.DevelopmentFunctions.error_log_trigger_error" />
</rule>

<rule ref="Inpsyde.CodeQuality.Psr4">
<properties>
<property
name="psr4"
type="array"
value="
Inpsyde\Modularity=>src,
Inpsyde\Modularity\Tests=>tests/src,
Inpsyde\Modularity\Tests\Unit=>tests/unit"
/>
</properties>
<rule ref="Syde-Extra">
<exclude name="SlevomatCodingStandard.Functions.RequireTrailingCommaInCall.MissingTrailingComma" />
<exclude name="SlevomatCodingStandard.Functions.RequireTrailingCommaInDeclaration.MissingTrailingComma" />
</rule>

<rule ref="Inpsyde.CodeQuality.FunctionLength">
<exclude-pattern>*/tests/*</exclude-pattern>
</rule>
<rule ref="Inpsyde.CodeQuality.ForbiddenPublicProperty">
<exclude-pattern>*/tests/*</exclude-pattern>
</rule>
<rule ref="WordPress.PHP.DevelopmentFunctions">
<exclude-pattern>*/tests/*</exclude-pattern>
</rule>
<rule ref="WordPress.PHP.DiscouragedPHPFunctions">
<exclude-pattern>*/tests/*</exclude-pattern>
</rule>
<rule ref="WordPress.Security.EscapeOutput">
<exclude-pattern>*/tests/*</exclude-pattern>
</rule>
<rule ref="Squiz.PHP.Eval">
<exclude-pattern>*/tests/*</exclude-pattern>
</rule>
</ruleset>
<rule ref="SlevomatCodingStandard.Files.TypeNameMatchesFileName">
<properties>
<property name="rootNamespaces" type="array">
<element key="src" value="Inpsyde\Modularity" />
<element key="tests/phpunit/Unit" value="Inpsyde\Modularity\Tests\Unit" />
</property>
</properties>
</rule>
</ruleset>
11 changes: 11 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
includes:
- vendor/szepeviktor/phpstan-wordpress/extension.neon
parameters:
level: 8
paths:
- src/
treatPhpDocTypesAsCertain: false
ignoreErrors:
- '#Trait Inpsyde\\Modularity\\Module\\ModuleClassNameIdTrait is used zero times and is not analysed.#'
# TODO: Check how we can point ABSPATH constant in phpstan to vendor/roots/wordpress-no-content/
- '#Path in require_once\(\) \".*\" is not a file or it does not exist.#'
2 changes: 1 addition & 1 deletion src/Container/ReadOnlyContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ReadOnlyContainer implements ContainerInterface
/**
* @param array<string, Service> $services
* @param array<string, bool> $factoryIds
* @param ServiceExtensions|array $extensions
* @param ServiceExtensions|array<string, ExtendingService> $extensions
* @param ContainerInterface[] $containers
*/
public function __construct(
Expand Down
25 changes: 19 additions & 6 deletions src/Container/ServiceExtensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class ServiceExtensions

/**
* @param string $type
*
* @return string
*/
final public static function typeId(string $type): string
Expand All @@ -30,18 +31,22 @@ final public static function typeId(string $type): string
/**
* @param string $extensionId
* @param ExtendingService $extender
*
* @return static
*/
public function add(string $extensionId, callable $extender): ServiceExtensions
{
isset($this->extensions[$extensionId]) or $this->extensions[$extensionId] = [];
if (!isset($this->extensions[$extensionId])) {
$this->extensions[$extensionId] = [];
}
$this->extensions[$extensionId][] = $extender;

return $this;
}

/**
* @param string $extensionId
*
* @return bool
*/
public function has(string $extensionId): bool
Expand All @@ -53,6 +58,7 @@ public function has(string $extensionId): bool
* @param mixed $service
* @param string $id
* @param Container $container
*
* @return mixed
*/
final public function resolve($service, string $id, Container $container)
Expand All @@ -68,6 +74,7 @@ final public function resolve($service, string $id, Container $container)
* @param string $id
* @param mixed $service
* @param Container $container
*
* @return mixed
*/
protected function resolveById(string $id, $service, Container $container)
Expand All @@ -83,11 +90,12 @@ protected function resolveById(string $id, $service, Container $container)
* @param string $className
* @param object $service
* @param Container $container
* @param array $extendedClasses
* @param string[] $extendedClasses
*
* @return mixed
*
* phpcs:disable Generic.Metrics.CyclomaticComplexity
* phpcs:disable Inpsyde.CodeQuality.ReturnTypeDeclaration
* phpcs:disable SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh
* phpcs:disable Syde.Functions.ReturnTypeDeclaration.NoReturnType
*/
protected function resolveByType(
string $className,
Expand All @@ -111,7 +119,9 @@ protected function resolveByType(

// 2nd group of extensions: targeting parent classes
$parents = class_parents($service, false);
($parents === false) and $parents = [];
if ($parents === false) {
$parents = [];
}
foreach ($parents as $parentName) {
$byParent = $this->extensions[self::typeId($parentName)] ?? null;
if (($byParent !== null) && ($byParent !== [])) {
Expand All @@ -121,7 +131,9 @@ protected function resolveByType(

// 3rd group of extensions: targeting implemented interfaces
$interfaces = class_implements($service, false);
($interfaces === false) and $interfaces = [];
if ($interfaces === false) {
$interfaces = [];
}
foreach ($interfaces as $interfaceName) {
$byInterface = $this->extensions[self::typeId($interfaceName)] ?? null;
if (($byInterface !== null) && ($byInterface !== [])) {
Expand Down Expand Up @@ -163,6 +175,7 @@ protected function resolveByType(
* @param object $service
* @param Container $container
* @param list<ExtendingService> $extenders
*
* @return list{mixed, int}
*/
private function extendByType(
Expand Down
16 changes: 10 additions & 6 deletions src/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

use Inpsyde\Modularity\Container\ContainerConfigurator;
use Inpsyde\Modularity\Container\PackageProxyContainer;
use Inpsyde\Modularity\Module\ExtendingModule;
use Inpsyde\Modularity\Module\ExecutableModule;
use Inpsyde\Modularity\Module\ExtendingModule;
use Inpsyde\Modularity\Module\FactoryModule;
use Inpsyde\Modularity\Module\Module;
use Inpsyde\Modularity\Module\ServiceModule;
Expand All @@ -17,6 +17,8 @@
/**
* @psalm-import-type Service from \Inpsyde\Modularity\Module\ServiceModule
* @psalm-import-type ExtendingService from \Inpsyde\Modularity\Module\ExtendingModule
*
* phpcs:disable WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
*/
class Package
{
Expand Down Expand Up @@ -210,7 +212,7 @@ public static function new(Properties $properties, ContainerInterface ...$contai

/**
* @param Properties $properties
* @param list<ContainerInterface> $containers
* @param ContainerInterface $containers
*/
private function __construct(Properties $properties, ContainerInterface ...$containers)
{
Expand Down Expand Up @@ -511,7 +513,7 @@ private function addModuleServices(Module $module, string $status): bool
{
/** @var null|array<string, Service|ExtendingService> $services */
$services = null;
/** @var null|callable(string, Service|ExtendingService) $addCallback */
/** @var null|callable(string, Service|ExtendingService): void $addCallback */
$addCallback = null;
switch ($status) {
case self::MODULE_REGISTERED:
Expand Down Expand Up @@ -552,7 +554,7 @@ private function doExecute(): void
$success = $executable->run($this->container());
$this->moduleProgress(
$executable->id(),
$success ? self::MODULE_EXECUTED : self::MODULE_EXECUTION_FAILED
$success ? self::MODULE_EXECUTED : self::MODULE_EXECUTION_FAILED,
);
}
}
Expand All @@ -569,7 +571,9 @@ private function moduleProgress(
?array $serviceIds = null
): void {

isset($this->moduleStatus[$status]) or $this->moduleStatus[$status] = [];
if (!isset($this->moduleStatus[$status])) {
$this->moduleStatus[$status] = [];
}
$this->moduleStatus[$status][] = $moduleId;

if (($serviceIds === null) || ($serviceIds === []) || !$this->properties->isDebug()) {
Expand Down Expand Up @@ -740,7 +744,7 @@ private function progress(int $status): void
* @param string $packageName
* @param string $reason
* @param bool $throw
* @return ($throw is true ? never: false)
* @return bool
*/
private function handleConnectionFailure(string $packageName, string $reason, bool $throw): bool
{
Expand Down
20 changes: 16 additions & 4 deletions src/Properties/BaseProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Inpsyde\Modularity\Properties;

/**
* phpcs:disable PHPCompatibility.FunctionDeclarations.RemovedImplicitlyNullableParam.Deprecated
*/
class BaseProperties implements Properties
{
protected ?bool $isDebug = null;
Expand Down Expand Up @@ -40,11 +43,14 @@ protected function __construct(

/**
* @param string $name
*
* @return lowercase-string
*/
protected function sanitizeBaseName(string $name): string
{
substr_count($name, '/') and $name = dirname($name);
if (substr_count($name, '/')) {
$name = dirname($name);
}

return strtolower(pathinfo($name, PATHINFO_FILENAME));
}
Expand Down Expand Up @@ -144,7 +150,9 @@ public function requiresWp(): ?string
{
$value = $this->get(self::PROP_REQUIRES_WP);

return (($value !== '') && is_string($value)) ? $value : null;
return (($value !== '') && is_string($value))
? $value
: null;
}

/**
Expand All @@ -154,11 +162,13 @@ public function requiresPhp(): ?string
{
$value = $this->get(self::PROP_REQUIRES_PHP);

return (($value !== '') && is_string($value)) ? $value : null;
return (($value !== '') && is_string($value))
? $value
: null;
}

/**
* @return array
* @return string[]
*/
public function tags(): array
{
Expand All @@ -168,6 +178,7 @@ public function tags(): array
/**
* @param string $key
* @param mixed $default
*
* @return mixed
*/
public function get(string $key, $default = null)
Expand All @@ -177,6 +188,7 @@ public function get(string $key, $default = null)

/**
* @param string $key
*
* @return bool
*/
public function has(string $key): bool
Expand Down
Loading

0 comments on commit ff304a2

Please sign in to comment.