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
{
// ...
}
public function example(bool $flag): int
{
if ($flag) { // counts
return 1; // counts
} // brace-only: does not count
return 0; // counts
}
// lines: 3
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: 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)
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.