|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use Castor\Attribute\AsTask; |
| 6 | +use function Castor\io; |
| 7 | +use function Castor\run; |
| 8 | + |
| 9 | +#[AsTask(description: 'Run mutation testing')] |
| 10 | +function infect(int $minMsi = 0, int $minCoveredMsi = 0, bool $ci = false): void |
| 11 | +{ |
| 12 | + io()->title('Running infection'); |
| 13 | + $nproc = run('nproc', quiet: true); |
| 14 | + if (! $nproc->isSuccessful()) { |
| 15 | + io()->error('Cannot determine the number of processors'); |
| 16 | + return; |
| 17 | + } |
| 18 | + $threads = (int) $nproc->getOutput(); |
| 19 | + $command = [ |
| 20 | + 'php', |
| 21 | + 'vendor/bin/infection', |
| 22 | + sprintf('--min-msi=%s', $minMsi), |
| 23 | + sprintf('--min-covered-msi=%s', $minCoveredMsi), |
| 24 | + sprintf('--threads=%s', $threads), |
| 25 | + ]; |
| 26 | + if ($ci) { |
| 27 | + $command[] = '--logger-github'; |
| 28 | + $command[] = '-s'; |
| 29 | + } |
| 30 | + $environment = [ |
| 31 | + 'XDEBUG_MODE' => 'coverage', |
| 32 | + ]; |
| 33 | + run($command, environment: $environment); |
| 34 | +} |
| 35 | + |
| 36 | +#[AsTask(description: 'Run tests')] |
| 37 | +function test(bool $coverageHtml = false, bool $coverageText = false, null|string $group = null): void |
| 38 | +{ |
| 39 | + io()->title('Running tests'); |
| 40 | + $command = ['php', 'vendor/bin/phpunit', '--color']; |
| 41 | + $environment = [ |
| 42 | + 'XDEBUG_MODE' => 'off', |
| 43 | + ]; |
| 44 | + if ($coverageHtml) { |
| 45 | + $command[] = '--coverage-html=build/coverage'; |
| 46 | + $environment['XDEBUG_MODE'] = 'coverage'; |
| 47 | + } |
| 48 | + if ($coverageText) { |
| 49 | + $command[] = '--coverage-text'; |
| 50 | + $environment['XDEBUG_MODE'] = 'coverage'; |
| 51 | + } |
| 52 | + if ($group !== null) { |
| 53 | + $command[] = sprintf('--group=%s', $group); |
| 54 | + } |
| 55 | + run($command, environment: $environment); |
| 56 | +} |
| 57 | + |
| 58 | +#[AsTask(description: 'Coding standards check')] |
| 59 | +function cs(bool $fix = false): void |
| 60 | +{ |
| 61 | + io()->title('Running coding standards check'); |
| 62 | + $command = ['php', 'vendor/bin/ecs', 'check']; |
| 63 | + $environment = [ |
| 64 | + 'XDEBUG_MODE' => 'off', |
| 65 | + ]; |
| 66 | + if ($fix) { |
| 67 | + $command[] = '--fix'; |
| 68 | + } |
| 69 | + run($command, environment: $environment); |
| 70 | +} |
| 71 | + |
| 72 | +#[AsTask(description: 'Running PHPStan')] |
| 73 | +function stan(): void |
| 74 | +{ |
| 75 | + io()->title('Running PHPStan'); |
| 76 | + $command = ['php', 'vendor/bin/phpstan', 'analyse']; |
| 77 | + $environment = [ |
| 78 | + 'XDEBUG_MODE' => 'off', |
| 79 | + ]; |
| 80 | + run($command, environment: $environment); |
| 81 | +} |
| 82 | + |
| 83 | +#[AsTask(description: 'Validate Composer configuration')] |
| 84 | +function validate(): void |
| 85 | +{ |
| 86 | + io()->title('Validating Composer configuration'); |
| 87 | + $command = ['composer', 'validate', '--strict']; |
| 88 | + $environment = [ |
| 89 | + 'XDEBUG_MODE' => 'off', |
| 90 | + ]; |
| 91 | + run($command, environment: $environment); |
| 92 | + |
| 93 | + $command = ['composer', 'dump-autoload', '--optimize', '--strict-psr']; |
| 94 | + run($command, environment: $environment); |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * @param array<string> $allowedLicenses |
| 99 | + */ |
| 100 | +#[AsTask(description: 'Check licenses')] |
| 101 | +function checkLicenses( |
| 102 | + array $allowedLicenses = ['Apache-2.0', 'BSD-2-Clause', 'BSD-3-Clause', 'ISC', 'MIT', 'MPL-2.0', 'OSL-3.0'] |
| 103 | +): void { |
| 104 | + io()->title('Checking licenses'); |
| 105 | + $allowedExceptions = []; |
| 106 | + $command = ['composer', 'licenses', '-f', 'json']; |
| 107 | + $environment = [ |
| 108 | + 'XDEBUG_MODE' => 'off', |
| 109 | + ]; |
| 110 | + $result = run($command, environment: $environment, quiet: true); |
| 111 | + if (! $result->isSuccessful()) { |
| 112 | + io()->error('Cannot determine licenses'); |
| 113 | + exit(1); |
| 114 | + } |
| 115 | + $licenses = json_decode($result->getOutput(), true); |
| 116 | + $disallowed = array_filter( |
| 117 | + $licenses['dependencies'], |
| 118 | + static fn (array $info, $name) => ! in_array($name, $allowedExceptions, true) |
| 119 | + && count(array_diff($info['license'], $allowedLicenses)) === 1, |
| 120 | + \ARRAY_FILTER_USE_BOTH |
| 121 | + ); |
| 122 | + $allowed = array_filter( |
| 123 | + $licenses['dependencies'], |
| 124 | + static fn (array $info, $name) => in_array($name, $allowedExceptions, true) |
| 125 | + || count(array_diff($info['license'], $allowedLicenses)) === 0, |
| 126 | + \ARRAY_FILTER_USE_BOTH |
| 127 | + ); |
| 128 | + if (count($disallowed) > 0) { |
| 129 | + io() |
| 130 | + ->table( |
| 131 | + ['Package', 'License'], |
| 132 | + array_map( |
| 133 | + static fn ($name, $info) => [$name, implode(', ', $info['license'])], |
| 134 | + array_keys($disallowed), |
| 135 | + $disallowed |
| 136 | + ) |
| 137 | + ); |
| 138 | + io() |
| 139 | + ->error('Disallowed licenses found'); |
| 140 | + exit(1); |
| 141 | + } |
| 142 | + io() |
| 143 | + ->table( |
| 144 | + ['Package', 'License'], |
| 145 | + array_map( |
| 146 | + static fn ($name, $info) => [$name, implode(', ', $info['license'])], |
| 147 | + array_keys($allowed), |
| 148 | + $allowed |
| 149 | + ) |
| 150 | + ); |
| 151 | + io() |
| 152 | + ->success('All licenses are allowed'); |
| 153 | +} |
| 154 | + |
| 155 | +#[AsTask(description: 'Run Rector')] |
| 156 | +function rector(bool $fix = false): void |
| 157 | +{ |
| 158 | + io()->title('Running Rector'); |
| 159 | + $command = ['php', 'vendor/bin/rector', 'process', '--ansi']; |
| 160 | + if (! $fix) { |
| 161 | + $command[] = '--dry-run'; |
| 162 | + } |
| 163 | + $environment = [ |
| 164 | + 'XDEBUG_MODE' => 'off', |
| 165 | + ]; |
| 166 | + run($command, environment: $environment); |
| 167 | +} |
| 168 | + |
| 169 | +#[AsTask(description: 'Run Rector')] |
| 170 | +function deptrac(): void |
| 171 | +{ |
| 172 | + io()->title('Running Rector'); |
| 173 | + $command = ['php', 'vendor/bin/deptrac', 'analyse', '--fail-on-uncovered', '--no-cache']; |
| 174 | + $environment = [ |
| 175 | + 'XDEBUG_MODE' => 'off', |
| 176 | + ]; |
| 177 | + run($command, environment: $environment); |
| 178 | +} |
0 commit comments