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
111 changes: 20 additions & 91 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,96 +2,39 @@

A static-analysis linter for DocBook XML files. It scans XML documentation sources and reports style and convention violations.

## Requirements
**Full documentation:** [jordikroon.github.io/docbook-cs](https://jordikroon.github.io/docbook-cs)

---

## Contributing

### Requirements

- PHP 8.5+
- Extensions: `dom`, `libxml`, `simplexml`

## Installation
### Setup

```bash
composer require jordikroon/docbookcs
composer install
```

## Usage
### Running checks

```bash
# Run with the default config file (docbookcs.xml in the current directory)
vendor/bin/docbook-cs

# Specify a config file
vendor/bin/docbook-cs --config=myconfig.xml

# Scan specific paths (overrides paths in config)
vendor/bin/docbook-cs reference/ language/

# Output as Checkstyle XML (useful for CI)
vendor/bin/docbook-cs --report=checkstyle --no-colors > report.xml

# Output as JSON
vendor/bin/docbook-cs --report=json

# Suppress progress output
vendor/bin/docbook-cs --quiet
```

### Exit codes

| Code | Meaning |
|------|---------|
| `0` | No violations found |
| `1` | One or more violations found |
| `2` | Runtime error (bad config, unreadable file, etc.) |

## Configuration
# Tests
vendor/bin/phpunit

Copy `docbookcs.xml.dist` to `docbookcs.xml` and adjust to your project:
# Static analysis
vendor/bin/phpstan

```xml
<?xml version="1.0" encoding="UTF-8"?>
<docbookcs>
<sniffs>
<sniff class="DocbookCS\Sniff\SimparaSniff" />
<sniff class="DocbookCS\Sniff\ExceptionNameSniff" />
<sniff class="DocbookCS\Sniff\AttributeOrderSniff" />
</sniffs>

<paths>
<path>reference</path>
<path>language</path>
</paths>

<exclude>
<pattern>*/skeleton.xml</pattern>
<pattern>*/.git/*</pattern>
</exclude>

<entities>
<directory>../doc-base/entities</directory>
<file>entities/global.ent</file>
</entities>
</docbookcs>
# Code style
vendor/bin/phpcs
```

### `<sniffs>`

Register sniffs by fully-qualified class name. Sniffs support optional `<property>` children for per-sniff configuration (see sniff documentation below).

### `<paths>`
### Writing a sniff

Directories or files to scan, relative to the config file or absolute. These are used when no paths are passed on the command line.

### `<exclude>`

`fnmatch`-style patterns for paths to skip.

### `<entities>`

Entity directories and files that should be loaded before parsing. Lets the XML parser resolve DocBook entities defined outside the scanned tree (e.g. from `doc-base`).

## Writing a custom sniff

Implement `DocbookCS\Sniff\SniffInterface` (or extend `AbstractSniff`) and register it in your config:
Implement `DocbookCS\Sniff\SniffInterface` (or extend `AbstractSniff`):

```php
namespace Acme\DocbookSniffs;
Expand All @@ -114,26 +57,12 @@ final class MySniff extends AbstractSniff
}
```

Register it in your config:

```xml
<sniff class="Acme\DocbookSniffs\MySniff" />
```

## Development

```bash
# Install dependencies
composer install

# Run tests
vendor/bin/phpunit

# Static analysis
vendor/bin/phpstan

# Code style
vendor/bin/phpcs
```

## License

Apache 2.0
5 changes: 4 additions & 1 deletion docbookcs.xml.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<docbookcs>
<docbookcs xmlns="https://jordikroon.github.io/docbook-cs/config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jordikroon.github.io/docbook-cs/config
https://jordikroon.github.io/docbook-cs/config.xsd">
<sniffs>
<sniff class="DocbookCS\Sniff\SimparaSniff">
<!-- Optionally add extra block elements that your project uses -->
Expand Down
2 changes: 1 addition & 1 deletion src/Config/ConfigParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

final class ConfigParser
{
private const string NAMESPACE_URI = 'https://docbookcs.org/config';
private const string NAMESPACE_URI = 'https://jordikroon.github.io/docbook-cs/config';

/**
* @throws ConfigParserException if the file cannot be read or contains invalid XML.
Expand Down
4 changes: 2 additions & 2 deletions src/Report/Reporter/ConsoleReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@ private function buildPerformance(Report $report): string

private function formatSeverity(Severity $severity): string
{
return match ($severity) {
return match ($severity) { // @codeCoverageIgnore
Severity::ERROR => $this->red(str_pad(Severity::ERROR->name, 7)),
Severity::WARNING => $this->yellow(str_pad(Severity::WARNING->name, 7)),
default => $this->dim(str_pad(strtoupper($severity->name), 7)),
};
}; // @codeCoverageIgnore
}

private function bold(string $text): string
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Sniff/SimparaSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public function itStillFlagsParasOutsideFormalparaWhenSiblingIsFormalpara(): voi
public function itDoesNotFlagParaInFormalparaRegardlessOfCase(): void
{
$doc = $this->createDocument(
'<root xmlns="http://docbook.org/ns/docbook">
'<root>
<formalpara>
<title>Title</title>
<para>Text</para>
Expand Down
Loading