Skip to content
Open
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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ jobs:
- name: "Install dependencies"
run: "composer install --no-interaction --prefer-dist"

- name: "Run PHP_CodeSniffer"
- name: "Run PHP-CS-Fixer"
run: |
set -o pipefail
vendor/bin/phpcs -q --report=checkstyle | cs2pr
vendor/bin/php-cs-fixer check --diff --format=checkstyle | cs2pr

stan:
name: "Static Analysis"
Expand Down
4 changes: 0 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/.phpcs-cache.json
/.phpstan.cache/
/.phpunit.cache/

/vendor/
/composer.lock

Expand Down
20 changes: 20 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

return new PhpCsFixer\Config()
->setRiskyAllowed(true)
->setRules([
'@PER-CS3x0' => true,
'@PHP8x4Migration' => true,
])
->setFinder(new PhpCsFixer\Finder()
->in(__DIR__ . '/src')
->append([
__FILE__,
__DIR__ . '/bin/docbook-cs',
]))
->setParallelConfig(PhpCsFixer\Runner\Parallel\ParallelConfigFactory::detect())
->setCacheFile(__DIR__ . '/var/.php-cs-fixer.cache')
->setUnsupportedPhpVersionAllowed(true)
;
20 changes: 13 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,23 @@ A static-analysis linter for DocBook XML files. It scans XML documentation sourc
composer install
```

### Running checks
### Tooling

```bash
# Tests
vendor/bin/phpunit
# run tests
composer test

# Static analysis
vendor/bin/phpstan
# run static analysis
composer phpstan

# Code style
vendor/bin/phpcs
# format all files
composer fixer -- fix

# format all dirty files
composer fixer-fresh

# check all files; do not apply
composer fixer -- check
```

### Writing a sniff
Expand Down
13 changes: 7 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
"ext-simplexml": "*"
},
"require-dev": {
"phpunit/phpunit": "^13.2.4",
"friendsofphp/php-cs-fixer": "^3.95",
"phpstan/phpstan": "^2.2.5",
"phpunit/php-code-coverage": "^14.2.3",
"squizlabs/php_codesniffer": "^4.0.2",
"phpstan/phpstan-strict-rules": "^2.0.12",
"phpstan/phpstan-phpunit": "^2.0.18",
"phpstan/phpstan-strict-rules": "^2.0.12",
"phpunit/phpunit": "^13.2.4",
"phpunit/php-code-coverage": "^14.2.3",
"shipmonk/dead-code-detector": "^1.3.2"
},
"autoload": {
Expand All @@ -32,9 +32,10 @@
"scripts": {
"test": "@php -r \"exit(extension_loaded('xdebug') || extension_loaded('pcov') ? 0 : 1);\" && XDEBUG_MODE=coverage ./vendor/bin/phpunit --configuration phpunit.xml.dist || ./vendor/bin/phpunit --configuration phpunit.xml.dist --no-coverage",
"phpstan": "./vendor/bin/phpstan analyse --no-progress",
"phpcs": "./vendor/bin/phpcs",
"fixer": "./vendor/bin/php-cs-fixer",
"fixer-fresh": "files=($(git --no-pager diff --name-only --diff-filter=ACMR HEAD | grep -E '\\.php$')); if [ ${#files[@]} -gt 0 ]; then vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php --path-mode=intersection -- \"${files[@]}\"; fi",
"quality": [
"@phpcs",
"@fixer-fresh",
"@phpstan",
"@test"
]
Expand Down
18 changes: 0 additions & 18 deletions phpcs.xml

This file was deleted.

76 changes: 38 additions & 38 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public static function withArguments(array $argv): self
return new self($argv, unifiedDiff: $stdin);
}

$type = $stat['mode'] & 0170000;
$type = $stat['mode'] & 0o170000;

if ($type === 0010000 || $type === 0100000) {
if ($type === 0o010000 || $type === 0o100000) {
$stdin = stream_get_contents(STDIN);

if ($stdin === false) {
Expand Down Expand Up @@ -350,42 +350,42 @@ private function isInteractive($stream): bool
private function printHelp(): void
{
$help = <<<'HELP'
DocbookCS - DocBook Code Sniffer

Usage:
docbook-cs [options] [<file-or-directory> ...]

Options:
-h, --help Show this help message and exit.
-v, --version Show version information and exit.
-q, --quiet Suppress progress output.
--config=<file> Path to configuration file (default: docbookcs.xml).
--report=<format> Output format: console (default), checkstyle, json.
--colors Force ANSI color output.
--no-colors Disable ANSI color output.
--fix Automatically fix violations when fixers exist
(experimental).
--wide Check whole selected files and recursively include
referenced XML files.

Arguments:
<file-or-directory> One or more files or directories to scan.
Paths cannot be combined with diff input.

Examples:
docbook-cs
docbook-cs --config=myconfig.xml reference/
docbook-cs --report=checkstyle --no-colors > report.xml
docbook-cs . --fix
docbook-cs reference/
docbook-cs reference/strings/functions/strlen.xml
docbook-cs reference/strings/functions/strlen.xml --wide
docbook-cs reference/strings/functions/strlen.xml --wide --fix
git diff HEAD | docbook-cs
git diff HEAD | docbook-cs --wide
git diff HEAD | docbook-cs --wide --fix --report=checkstyle

HELP;
DocbookCS - DocBook Code Sniffer

Usage:
docbook-cs [options] [<file-or-directory> ...]

Options:
-h, --help Show this help message and exit.
-v, --version Show version information and exit.
-q, --quiet Suppress progress output.
--config=<file> Path to configuration file (default: docbookcs.xml).
--report=<format> Output format: console (default), checkstyle, json.
--colors Force ANSI color output.
--no-colors Disable ANSI color output.
--fix Automatically fix violations when fixers exist
(experimental).
--wide Check whole selected files and recursively include
referenced XML files.

Arguments:
<file-or-directory> One or more files or directories to scan.
Paths cannot be combined with diff input.

Examples:
docbook-cs
docbook-cs --config=myconfig.xml reference/
docbook-cs --report=checkstyle --no-colors > report.xml
docbook-cs . --fix
docbook-cs reference/
docbook-cs reference/strings/functions/strlen.xml
docbook-cs reference/strings/functions/strlen.xml --wide
docbook-cs reference/strings/functions/strlen.xml --wide --fix
git diff HEAD | docbook-cs
git diff HEAD | docbook-cs --wide
git diff HEAD | docbook-cs --wide --fix --report=checkstyle

HELP;

$this->write($help);
}
Expand Down
3 changes: 1 addition & 2 deletions src/Config/ConfigData.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ public function __construct(
private array $excludePatterns,
private array $entityPaths,
private string $basePath,
) {
}
) {}

/** @return array<string, string> */
public function getProjectRoots(): array
Expand Down
16 changes: 7 additions & 9 deletions src/Config/ConfigParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ final class ConfigParser
{
private const string NAMESPACE_URI = 'https://php.github.io/docbook-cs/config';

public function __construct(private readonly XmlParser $xmlParser = new XmlParser())
{
}
public function __construct(private readonly XmlParser $xmlParser = new XmlParser()) {}

/**
* @throws ConfigParserException if the file cannot be read or contains invalid XML.
Expand Down Expand Up @@ -115,16 +113,16 @@ private function parseSniffs(\SimpleXMLElement $root): array
$entries = [];

foreach ($sniffsNode->sniff as $sniffNode) {
$class = (string)($sniffNode['class'] ?? '');
$class = (string) ($sniffNode['class'] ?? '');

if ($class === '') {
throw ConfigParserException::missingAttribute('sniff', 'class');
}

$properties = [];
foreach ($sniffNode->property as $prop) {
$name = (string)($prop['name'] ?? '');
$value = (string)($prop['value'] ?? '');
$name = (string) ($prop['name'] ?? '');
$value = (string) ($prop['value'] ?? '');

if ($name === '') {
throw ConfigParserException::missingAttribute('property', 'name');
Expand All @@ -149,7 +147,7 @@ private function parsePaths(\SimpleXMLElement $root, string $basePath): array
}

foreach ($root->paths->path as $pathNode) {
$raw = trim((string)$pathNode);
$raw = trim((string) $pathNode);

if ($raw === '') {
continue;
Expand All @@ -171,7 +169,7 @@ private function parseExcludePatterns(\SimpleXMLElement $root): array
}

foreach ($root->exclude->pattern as $patternNode) {
$raw = trim((string)$patternNode);
$raw = trim((string) $patternNode);

if ($raw !== '') {
$patterns[] = $raw;
Expand All @@ -191,7 +189,7 @@ private function parseEntityPaths(\SimpleXMLElement $root, string $basePath): ar
}

foreach ($root->entities->children() as $node) {
$raw = trim((string)$node);
$raw = trim((string) $node);
if ($raw === '') {
continue;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Diff/DiffBaseResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
public function __construct(
private GitClient $git,
private UpstreamResolver $officialUpstream,
) {
}
) {}

/** @throws GitException */
public function resolve(string $repoRoot): string
Expand Down
4 changes: 1 addition & 3 deletions src/Diff/DiffChangeset.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
final readonly class DiffChangeset
{
/** @param list<FileChange> $fileChanges */
public function __construct(public array $fileChanges)
{
}
public function __construct(public array $fileChanges) {}

public function changeFor(string $filePath): ?FileChange
{
Expand Down
3 changes: 1 addition & 2 deletions src/Diff/FileChange.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@ public function __construct(
public string $filePath,
public array $addedLineNumbers,
public array $deletionAnchors = [],
) {
}
) {}
}
2 changes: 1 addition & 1 deletion src/Diff/UpstreamResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ private function refreshAndResolve(string $repoRoot, string $repoName): ?string
private function prepareCacheDirectory(): bool
{
return is_dir($this->cacheDirectory)
|| @mkdir($this->cacheDirectory, 0777, recursive: true)
|| @mkdir($this->cacheDirectory, 0o777, recursive: true)
|| is_dir($this->cacheDirectory);
}

Expand Down
3 changes: 1 addition & 2 deletions src/Fix/Fix.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,5 @@ public function __construct(
public string $replacement,
public string $sniffCode,
public ?string $expectedContent = null,
) {
}
) {}
}
4 changes: 2 additions & 2 deletions src/Fix/FixApplier.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public function apply(File $file, array $fixes): FixResult
}

$plans = array_map(
static fn ($fix): FixPlan => $fix instanceof FixPlan ? $fix : new FixPlan($fix),
static fn($fix): FixPlan => $fix instanceof FixPlan ? $fix : new FixPlan($fix),
$fixes,
);

usort($plans, static fn (FixPlan $a, FixPlan $b): int => $a->firstOffset() <=> $b->firstOffset());
usort($plans, static fn(FixPlan $a, FixPlan $b): int => $a->firstOffset() <=> $b->firstOffset());

/** @var list<Fix> $acceptedFixes */
$acceptedFixes = [];
Expand Down
3 changes: 1 addition & 2 deletions src/Fix/FixResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@ public function __construct(
public int $applied = 0,
public int $skipped = 0,
public array $appliedFixes = [],
) {
}
) {}
}
4 changes: 1 addition & 3 deletions src/Git/GitClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
{
private const string FORCED_BRANCH_REF_SPEC = '+refs/heads/%s:%s';

public function __construct(private ProcessRunnerInterface $processRunner)
{
}
public function __construct(private ProcessRunnerInterface $processRunner) {}

/** @throws GitException */
public function repoRoot(string $workingDirectory): string
Expand Down
3 changes: 1 addition & 2 deletions src/Path/DiffPathLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ public function __construct(
private string $basePath,
private array $projectRoots,
private PathMatcher $matcher,
) {
}
) {}

public function load(): DiffChangeset
{
Expand Down
2 changes: 1 addition & 1 deletion src/Path/EntityResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __construct(
array $extensions = ['ent', 'dtd']
) {
$this->extensions = array_values(
array_map(static fn (string $extension): string => ltrim($extension, '.'), $extensions)
array_map(static fn(string $extension): string => ltrim($extension, '.'), $extensions)
);
}

Expand Down
3 changes: 1 addition & 2 deletions src/Path/PathMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
public function __construct(
private string $basePath,
private array $excludePatterns
) {
}
) {}

public function isExcluded(string $filePath): bool
{
Expand Down
3 changes: 1 addition & 2 deletions src/Process/ProcessResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ public function __construct(
public int $exitCode,
public string $stdout,
public string $stderr,
) {
}
) {}
}
Loading