Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
* text=auto eol=lf

/.github export-ignore
/tests export-ignore
/docs export-ignore
/AGENTS.md export-ignore
/CONTRIBUTING.md export-ignore
/qa-adoption.json export-ignore
/phpcs.xml export-ignore
/phpcs.xml.dist export-ignore
/phpstan.neon export-ignore
/phpstan.neon.dist export-ignore
/phpunit*.xml* export-ignore
/.php-cs-fixer* export-ignore
/.gitignore export-ignore
/.gitattributes export-ignore
/.env* export-ignore
/auth.json export-ignore
/.npmrc export-ignore
/composer.review*.json export-ignore
/composer.review*.lock export-ignore
18 changes: 18 additions & 0 deletions .github/workflows/dependency-canary.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Dependency compatibility

on:
schedule:
- cron: '23 3 * * 3'
workflow_dispatch:

permissions:
contents: read

jobs:
compatibility:
permissions:
contents: read
issues: write
uses: sympress/workflows/.github/workflows/dependency-canary.yml@177fa0d727b278d2103052ec77c102b4a4c492a0
with:
php_version: '8.5'
4 changes: 2 additions & 2 deletions .github/workflows/qa.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ permissions:

jobs:
workflows:
uses: sympress/workflows/.github/workflows/lint-workflows.yml@v1
uses: sympress/workflows/.github/workflows/lint-workflows.yml@177fa0d727b278d2103052ec77c102b4a4c492a0

qa:
uses: sympress/workflows/.github/workflows/sympress-qa.yml@v1
uses: sympress/workflows/.github/workflows/sympress-qa.yml@177fa0d727b278d2103052ec77c102b4a4c492a0
with:
php_version: '8.5'
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ vendor/bin/qa qa --strict
vendor/bin/qa doctor --strict
```

Unavailable gates are visibly reported as `SKIP` by default. Add `--strict` to a
Local unavailable gates are visibly reported as `SKIP`. CI and GitHub Actions
fail unavailable enabled gates automatically. Add `--strict` to a local
tool command or to `qa` when an unavailable gate must fail. Use
`doctor --strict` with an adoption file to verify that required gates are
configured and referenced by `composer qa`.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"symfony/filesystem": "^8.0",
"symfony/process": "^8.0",
"szepeviktor/phpstan-wordpress": "^2.0",
"sympress/coding-standards": "^1.1"
"sympress/coding-standards": "^1.1.1"
},
"autoload": {
"psr-4": {
Expand Down
6 changes: 6 additions & 0 deletions config/phpstan/wordpress.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ includes:
parameters:
level: 8
treatPhpDocTypesAsCertain: false

services:
-
class: SymPress\Qa\PHPStan\Rules\PreparedSqlRule
tags:
- phpstan.rules.rule
23 changes: 23 additions & 0 deletions docs/adoption.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,26 @@ Rules:
- Relative package directories such as `packages/example` can be used as fallback keys.

This lets the repository enforce `composer qa` everywhere while migrating static analysis and tests package by package.

## CI enforcement

`qa`, each tool command and `doctor` enable strict behavior when `CI` or
`GITHUB_ACTIONS` is truthy (`1`, `true`, `yes`, `on`, case insensitive).
Explicit `--strict` always enables it. Local non-strict adoption skips remain
visible. CI does not turn planned doctor gates into required gates.

## Typed WordPress SQL checking

Include `vendor/sympress/qa/config/phpstan/wordpress.neon` in the consumer's
PHPStan configuration. It registers `sympress.preparedSql` for query-bearing
methods on typed `wpdb` receivers, including properties, aliases and factories.
Constant SQL and direct typed `wpdb::prepare()` with a constant template pass.
Unknown SQL variables, interpolated templates, value concatenation and another
object's `prepare()` fail. Assigned prepared values are conservatively rejected;
this release does not claim SQL dataflow tracking. Structured insert/update/delete
APIs are unaffected. Vetted dynamic identifiers and explicit raw SQL boundaries
need a local `@phpstan-ignore sympress.preparedSql` with a review reason and
parameterization tests. Never ignore this identifier for a whole repository.

WPCS remains complementary token-based checking; see coding-standards docs for
its receiver-name limitation. The pure library PHPStan profile remains unchanged.
20 changes: 20 additions & 0 deletions docs/releases/0.1.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Candidate release 0.1.2

This candidate uses stable Composer constraints and the repaired SymPress
package train. It has not been published. The repository's QA workflow references
the reviewed immutable workflow commit; a weekly dependency canary resolves
current permitted stable dependencies and reports a failure or recovery through
a single GitHub issue.

Runtime files and resources remain in Composer archives. Tests, CI and local
configuration are excluded through `.gitattributes`. Historical tags remain
unchanged. The candidate becomes consumable from public registries only after
the dependency train has been published and exact-head CI/fresh installations
have passed.

For coordinated local development, provide an explicit root Composer repository
with the reviewed candidate versions. Do not restore library-level
`minimum-stability: dev` or moving `dev-main` dependencies. Local candidate
fixtures demonstrate source compatibility; they do not establish public release
availability. Application production installs require a committed lock and
`composer install --no-dev --optimize-autoloader --classmap-authoritative`.
1 change: 1 addition & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<file>tests</file>

<exclude-pattern>vendor/*</exclude-pattern>
<exclude-pattern>tests/Unit/PHPStan/fixtures/*</exclude-pattern>
<exclude-pattern>.phpunit.cache/*</exclude-pattern>

<rule ref="SymPress-Pure">
Expand Down
3 changes: 3 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
parameters:
level: max
excludePaths:
analyse:
- tests/Unit/PHPStan/fixtures/*
paths:
- bin/qa
- src
Expand Down
7 changes: 7 additions & 0 deletions src/Command/AbstractPackageCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ public function __construct(
parent::__construct($name);
}

protected function strict(InputInterface $input): bool
{
return $input->getOption('strict') === true
|| filter_var(getenv('CI'), FILTER_VALIDATE_BOOLEAN)
|| filter_var(getenv('GITHUB_ACTIONS'), FILTER_VALIDATE_BOOLEAN);
}

protected function configurePackageOption(): void
{
$this->addOption(
Expand Down
2 changes: 1 addition & 1 deletion src/Command/DoctorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$style->error($error);
}

return $input->getOption('strict') === true ? self::FAILURE : self::SUCCESS;
return $this->strict($input) ? self::FAILURE : self::SUCCESS;
}

/** @return array<string, mixed> */
Expand Down
2 changes: 1 addition & 1 deletion src/Command/QaCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$context = $this->packageContext($input);

foreach (['cs', 'static-analysis', 'tests'] as $gate) {
$exitCode = $this->toolRunner->run($gate, $context, $style, $input->getOption('strict') === true);
$exitCode = $this->toolRunner->run($gate, $context, $style, $this->strict($input));

if ($exitCode !== self::SUCCESS) {
return $exitCode;
Expand Down
2 changes: 1 addition & 1 deletion src/Command/ToolCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->gate,
$this->packageContext($input),
new SymfonyStyle($input, $output),
$input->getOption('strict') === true,
$this->strict($input),
);
}
}
84 changes: 84 additions & 0 deletions src/PHPStan/Rules/PreparedSqlRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);

namespace SymPress\Qa\PHPStan\Rules;

use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\ObjectType;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;

/** @implements Rule<MethodCall> */
final class PreparedSqlRule implements Rule
{
public function getNodeType(): string
{
return MethodCall::class;
}

public function processNode(Node $node, Scope $scope): array
{
if (!$node->name instanceof Identifier || !in_array(strtolower($node->name->name), ['query', 'get_var', 'get_col', 'get_row', 'get_results'], true)) {
return [];
}
if (!$this->wpdbReceiver($node->var, $scope)) {
return [];
}
$query = $this->query($node);
if ($query === null || $this->constant($query, $scope) || $this->prepared($query, $scope)) {
return [];
}

return [
RuleErrorBuilder::message('wpdb SQL must be constant SQL or a direct typed wpdb::prepare() call with a constant template; audit dynamic identifiers and raw SQL boundaries explicitly.')
->identifier('sympress.preparedSql')->build(),
];
}

private function wpdbReceiver(Node\Expr $receiver, Scope $scope): bool
{
foreach ($scope->getType($receiver)->getObjectClassNames() as $class) {
if ((new ObjectType('wpdb'))->isSuperTypeOf(new ObjectType($class))->yes()) {
return true;
}
}

return false;
}

private function query(MethodCall $call): ?Node\Expr
{
foreach ($call->getArgs() as $position => $argument) {
if ($argument->name?->name === 'query' || ($position === 0 && $argument->name === null)) {
return $argument->value;
}
}

return null;
}

private function constant(Node\Expr $query, Scope $scope): bool
{
$type = $scope->getType($query);

return $type->isConstantValue()->yes() && $type->isString()->yes();
}

private function prepared(Node\Expr $query, Scope $scope): bool
{
if (
!$query instanceof MethodCall || !$query->name instanceof Identifier
|| strtolower($query->name->name) !== 'prepare'
|| !(new ObjectType('wpdb'))->isSuperTypeOf($scope->getType($query->var))->yes()
) {
return false;
}
$template = $this->query($query);

return $template !== null && $this->constant($template, $scope);
}
}
11 changes: 8 additions & 3 deletions src/Runner/ToolRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use SymPress\Qa\Support\PackageContext;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Process\Exception\ProcessSignaledException;
use Symfony\Component\Process\Process;

final class ToolRunner
Expand Down Expand Up @@ -137,9 +138,13 @@ private function runTool(
: [];
$process = new Process(array_merge([PHP_BINARY], $phpArguments, [$bin], $arguments), $context->packageDir());
$process->setTimeout(null);
$process->run(static function (string $type, string $buffer) use ($style): void {
$style->write($buffer);
});
try {
$process->run(static function (string $type, string $buffer) use ($style): void {
$style->write($buffer);
});
} catch (ProcessSignaledException) {
return $process->getExitCode() ?? Command::FAILURE;
}

return $process->getExitCode() ?? Command::FAILURE;
}
Expand Down
41 changes: 41 additions & 0 deletions tests/Unit/Command/CiStrictnessTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace SymPress\Qa\Tests\Unit\Command;

use PHPUnit\Framework\TestCase;
use SymPress\Qa\Console\ApplicationFactory;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Filesystem\Filesystem;

final class CiStrictnessTest extends TestCase
{
public function testCiRequiresGatesWithoutStrictOptionAndLocalSkipsRemainVisible(): void
{
$workspace = sys_get_temp_dir() . '/qa-ci-' . bin2hex(random_bytes(8));
(new Filesystem())->mkdir($workspace);
file_put_contents($workspace . '/composer.json', '{"name":"sympress/fixture"}');
$ciEnvironment = getenv('CI');
$github = getenv('GITHUB_ACTIONS');
try {
foreach (['true', '1', 'yes', 'on', 'false', '0', 'no', 'off', ''] as $value) {
putenv('CI=' . $value);
putenv('GITHUB_ACTIONS=false');
foreach (['qa', 'cs', 'static-analysis', 'tests', 'doctor'] as $command) {
$tester = new CommandTester(ApplicationFactory::create()->find($command));
self::assertSame(filter_var($value, FILTER_VALIDATE_BOOLEAN) ? 1 : 0, $tester->execute(['--package' => $workspace]), $command . ': ' . $value);
}
}
putenv('CI=false');
putenv('GITHUB_ACTIONS=true');
$tester = new CommandTester(ApplicationFactory::create()->find('cs'));
self::assertSame(1, $tester->execute(['--package' => $workspace]));
self::assertStringContainsString('FAILED', $tester->getDisplay());
} finally {
putenv($ciEnvironment === false ? 'CI' : 'CI=' . $ciEnvironment);
putenv($github === false ? 'GITHUB_ACTIONS' : 'GITHUB_ACTIONS=' . $github);
(new Filesystem())->remove($workspace);
}
}
}
27 changes: 19 additions & 8 deletions tests/Unit/Command/DoctorCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,25 @@ protected function tearDown(): void

public function testStrictModeTurnsMissingRequiredGateIntoFailure(): void
{
$command = ApplicationFactory::create()->find('doctor');

$advisory = new CommandTester($command);
self::assertSame(0, $advisory->execute(['--package' => $this->workspace]));
self::assertStringContainsString('missing composer qa script', $advisory->getDisplay());

$strict = new CommandTester($command);
self::assertSame(1, $strict->execute(['--package' => $this->workspace, '--strict' => true]));
$ciEnvironment = getenv('CI');
$githubEnvironment = getenv('GITHUB_ACTIONS');
try {
// This case explicitly compares local advisory mode with --strict.
// CI strictness is exercised separately, so preserve hosted settings.
putenv('CI=false');
putenv('GITHUB_ACTIONS=false');
$command = ApplicationFactory::create()->find('doctor');

$advisory = new CommandTester($command);
self::assertSame(0, $advisory->execute(['--package' => $this->workspace]));
self::assertStringContainsString('missing composer qa script', $advisory->getDisplay());

$strict = new CommandTester($command);
self::assertSame(1, $strict->execute(['--package' => $this->workspace, '--strict' => true]));
} finally {
putenv($ciEnvironment === false ? 'CI' : 'CI=' . $ciEnvironment);
putenv($githubEnvironment === false ? 'GITHUB_ACTIONS' : 'GITHUB_ACTIONS=' . $githubEnvironment);
}
}

public function testInvalidAdoptionFileAlwaysFails(): void
Expand Down
Loading
Loading