-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prefer bin/split-phpstan-baseline, deprecate formatter approach (#10)
* Prefer bin/split-phpstan-baseline, deprecate formatter approach * Fix composer autoload path * Wording * Fix readme * Avoid confusion in readme * Fix deprecation message in formatter
- Loading branch information
Showing
9 changed files
with
284 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#!/usr/bin/env php | ||
<?php declare(strict_types=1); | ||
|
||
use Nette\Neon\Exception as NeonException; | ||
use Nette\Neon\Neon; | ||
use ShipMonk\PHPStan\Baseline\NeonHelper; | ||
|
||
$autoloadFiles = [ | ||
__DIR__ . '/../../../autoload.php', | ||
__DIR__ . '/../vendor/autoload.php', | ||
]; | ||
|
||
foreach ($autoloadFiles as $autoloadFile) { | ||
if (file_exists($autoloadFile)) { | ||
require_once $autoloadFile; | ||
break; | ||
} | ||
} | ||
|
||
$providedOptions = getopt('', ['tabs'], $restIndex); | ||
$args = array_slice($argv, $restIndex); | ||
|
||
$loaderFile = $args[0] ?? null; | ||
$indent = isset($providedOptions['tabs']) | ||
? "\t" | ||
: ' '; | ||
|
||
if ($loaderFile === null) { | ||
fwrite(STDERR, "\n! Missing argument of generated baseline file.\n\n"); | ||
exit(1); | ||
} | ||
|
||
if (!is_file($loaderFile)) { | ||
fwrite(STDERR, "\n! File '$loaderFile' not found\n\n"); | ||
exit(1); | ||
} | ||
|
||
$splFile = new SplFileInfo($loaderFile); | ||
|
||
$folder = $splFile->getPath(); | ||
$extension = $splFile->getExtension(); | ||
|
||
if ($extension !== 'neon') { | ||
fwrite(STDERR, "\n! Invalid file extension '$extension' of '$loaderFile', expected neon file\n\n"); | ||
exit(1); | ||
} | ||
|
||
try { | ||
$data = Neon::decodeFile($loaderFile); | ||
} catch (NeonException $e) { | ||
fwrite(STDERR, "\n! Invalid argument, expected a valid neon file: " . $e->getMessage() . "\n\n"); | ||
exit(1); | ||
} | ||
if (!isset($data['parameters']['ignoreErrors'])) { | ||
fwrite( | ||
STDERR, | ||
"\n! Invalid argument, expected neon file with 'parameters.ignoreErrors' key in '$loaderFile'." . | ||
"\n - Did you run native baseline generation first?" . | ||
"\n - You can so via vendor/bin/phpstan --generate-baseline=$loaderFile\n\n" | ||
); | ||
exit(1); | ||
} | ||
|
||
$groupedErrors = []; | ||
foreach ($data['parameters']['ignoreErrors'] as $error) { | ||
$identifier = $error['identifier'] ?? 'missing-identifier'; | ||
unset($error['identifier']); | ||
$groupedErrors[$identifier][] = $error; | ||
} | ||
|
||
ksort($groupedErrors); | ||
|
||
$loaderData = []; | ||
|
||
foreach ($groupedErrors as $identifier => $errors) { | ||
$filePath = $folder . '/' . $identifier . '.neon'; | ||
$loaderData['includes'][] = $identifier . '.neon'; | ||
$outputData = ['parameters' => ['ignoreErrors' => $errors]]; | ||
$errorsCount = count($errors); | ||
$plural = $errorsCount === 1 ? '' : 's'; | ||
$prefix = "# total $errorsCount error$plural\n\n"; | ||
$contents = $prefix . NeonHelper::encode($outputData, $indent); | ||
file_put_contents($filePath, $contents); | ||
echo "Writing baseline file $filePath with $errorsCount errors\n"; | ||
} | ||
|
||
file_put_contents($loaderFile, NeonHelper::encode($loaderData, $indent)); | ||
echo "Writing baseline loader to $loaderFile\n"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace ShipMonk\PHPStan\Baseline; | ||
|
||
use LogicException; | ||
use Nette\Neon\Neon; | ||
use function preg_replace; | ||
use function trim; | ||
|
||
class NeonHelper | ||
{ | ||
|
||
public static function encode(mixed $data, string $indent): string | ||
{ | ||
return trim(Neon::encode($data, blockMode: true, indentation: $indent)) . "\n"; | ||
} | ||
|
||
public static function escape(string $value): string | ||
{ | ||
$return = preg_replace('#^@|%#', '$0$0', $value); | ||
|
||
if ($return === null) { | ||
throw new LogicException('Error while escaping ' . $value); | ||
} | ||
|
||
return $return; | ||
} | ||
|
||
} |
Oops, something went wrong.