PHP code example of ianrodrigues / pest-plugin-code-quality

1. Go to this page and download the library: Download ianrodrigues/pest-plugin-code-quality library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ianrodrigues / pest-plugin-code-quality example snippets


arch('controllers remain easy to change')
    ->expect('App\Http\Controllers')
    ->classes()
    ->toHaveMethodComplexityAtMost(10)
    ->toHaveMethodLinesAtMost(40)
    ->toHaveMethodParametersAtMost(4);

arch('parsers stay within their complexity budget')
    ->expect('App\Parsing')
    ->classes()
    ->toHaveMethodComplexityAtMost(20);

arch('controllers behave, and stay small')
    ->expect('App\Http\Controllers')
    ->classes()
    ->toBeFinal()
    ->toHaveMethodComplexityAtMost(10)
    ->toHaveMethodLinesAtMost(40);

arch('controllers remain easy to change')
    ->expect('App\Http\Controllers')
    ->classes()
    ->toHaveMethodComplexityAtMost(10)
    ->ignoring('App\Http\Controllers\Generated');

/** @pest-arch-ignore-line */
public function legacyEntryPoint(array $payload): array
{
    // ...
}

   // tests/Pest.php
   \IanRodrigues\CodeQuality\Config::baseline(__DIR__.'/quality-baseline.json');
   

arch('a namespace that is allowed to be empty for now')
    ->expect('App\Experimental')
    ->classes()
    ->toHaveMethodComplexityAtMost(10, allowEmpty: true);

public function classify(?int $code, bool $strict): string
{
    if ($code === null) {              // if: +1
        return 'unknown';
    }

    $label = match (true) {
        $code < 0 => 'negative',       // match arm: +1
        $code === 0 => 'zero',         // match arm: +1
        default => 'positive',
    };

    return $strict && $label !== ''    // &&: +1
        ? strtoupper($label)           // ternary: +1
        : $label;
}
// ccn2: 1 + 1 + 1 + 1 + 1 + 1 = 6

public function example(bool $flag): int
{
    if ($flag) {           // counts
        return 1;           // counts
    }                        // brace-only: does not count

    return 0;                // counts
}
// lines: 3

public function __construct(
    private readonly string $name, // promoted: +1
    int $count = 0,                // optional: +1
    &$byRef = null,                // by-reference: +1
    ...$rest                       // variadic: +1
) {}
// params: 4

final class MethodsExample
{
    public function __construct(private int $size) {}   // +1

    public function size(): int                         // +1, an accessor
    {
        return $this->size;
    }

    public function grow(int $by): self                 // +1, an accessor
    {
        $this->size = $this->size + $by;

        return $this;
    }

    public function describe(): string                  // +1
    {
        return 'size '.$this->size;
    }
}
// methods: 4, and 2 with ignoringAccessors: true

final class PropertiesExample
{
    public const string KIND = 'example';       // a constant: +0

    public string $name = '';                   // +1

    public ?int $first = null, $second = null;  // +2

    public function __construct(private readonly int $size) {}  // promoted: +1
}
// properties: 4

final class InheritanceExample extends \RuntimeException
{
}
// inheritance: RuntimeException + Exception = 2

final class ClassLinesExample
{
    public int $size = 0;          // counts

    public function grow(): void   // counts
    {
        $this->size++;             // counts
    }                              // brace-only: does not count
}
// classLines: 3

final class OrderLineItemsProcessor
{
}
// className: 23

final class Invoice
{
    public function calculateOutstandingBalance(): float
    {
        return 0.0;
    }
}
// methodName: 27

final class Order
{
    public function total(int $quantity, float $unitPrice): float
    {
        $lineItemSubtotal = $quantity * $unitPrice;

        return $lineItemSubtotal;
    }
}
// variableName: 16, the identifier is lineItemSubtotal

Quality inspection

controllers remain easy to change
tests/ArchTest.php:8

Metric: ccn2 v1, limit 10
Targets: App\Http\Controllers
Directories searched: app/Http/Controllers
Exclusions: none
Files found: 1  Objects: 1  With AST: 1  Methods measured: 1
Measurements:
  App\Http\Controllers\CheckoutController::store (app/Http/Controllers/CheckoutController.php:9) ccn2=11 lines=20 params=3

parsers stay within their complexity budget
tests/ArchTest.php:15

Metric: ccn2 v1, limit 20
Targets: App\Parsing
Directories searched: app/Parsing
Exclusions: none
Files found: 1  Objects: 1  With AST: 1  Methods measured: 1
Measurements:
  App\Parsing\Parser::parse (app/Parsing/Parser.php:9) ccn2=5 lines=8 params=1

Quality: 3 files were found but not analysed (run with --quality-inspect for details)
  app/Billing/Old.php (not loadable)
  app/Billing/Refund.php (namespace mismatch)