|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace NelsonMartell\PhpCodeSniffer; |
| 6 | + |
| 7 | +use Composer\IO\IOInterface; |
| 8 | +use Composer\Script\Event; |
| 9 | + |
| 10 | +/** |
| 11 | + * Composer scripts helpers. |
| 12 | + */ |
| 13 | +class ComposerScripts |
| 14 | +{ |
| 15 | + /** |
| 16 | + * @var string|null |
| 17 | + */ |
| 18 | + protected static $binDir; |
| 19 | + |
| 20 | + /** |
| 21 | + * @var string|null |
| 22 | + */ |
| 23 | + protected static $vendorDir; |
| 24 | + |
| 25 | + protected static function getBinDir(Event $event) |
| 26 | + { |
| 27 | + if (!static::$binDir) { |
| 28 | + static::$binDir = realpath($event->getComposer()->getConfig()->get('bin-dir')); |
| 29 | + } |
| 30 | + |
| 31 | + return static::$binDir; |
| 32 | + } |
| 33 | + |
| 34 | + protected static function getVendorDir(Event $event) |
| 35 | + { |
| 36 | + if (!static::$vendorDir) { |
| 37 | + static::$vendorDir = realpath($event->getComposer()->getConfig()->get('vendor-dir')); |
| 38 | + } |
| 39 | + |
| 40 | + return static::$vendorDir; |
| 41 | + } |
| 42 | + |
| 43 | + protected static function bootstrap(Event $event) |
| 44 | + { |
| 45 | + require_once static::getVendorDir($event) . '/autoload.php'; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Custom PHP Code Sniffer Fixer to be run with lint-staged pre-commit hook. |
| 50 | + */ |
| 51 | + public static function phpcbf(Event $event): void |
| 52 | + { |
| 53 | + $start_time = microtime(true); |
| 54 | + |
| 55 | + static::bootstrap($event); |
| 56 | + |
| 57 | + $rootDir = realpath(getcwd()); |
| 58 | + |
| 59 | + $cmd = str_replace($rootDir . DIRECTORY_SEPARATOR, '', realpath(static::getBinDir($event) . '/phpcbf')); |
| 60 | + |
| 61 | + $files = $event->getArguments(); |
| 62 | + $count = count($files); |
| 63 | + |
| 64 | + $ignoredPaths = []; |
| 65 | + |
| 66 | + if ($count > 0) { |
| 67 | + $event->getIO()->write("Fixing PHP Coding Standard of ${count} paths."); |
| 68 | + |
| 69 | + foreach ($files as $i => $file) { |
| 70 | + $realPath = realpath($file); |
| 71 | + |
| 72 | + if (!$realPath) { |
| 73 | + $ignoredPaths[] = $file; |
| 74 | + continue; |
| 75 | + } |
| 76 | + |
| 77 | + // if ($realPath === realpath(__FILE__)) { |
| 78 | + // // Do not self-fix this file when lint-staged |
| 79 | + // continue; |
| 80 | + // } |
| 81 | + |
| 82 | + $relativePath = str_replace( |
| 83 | + [$rootDir . DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR], |
| 84 | + ['', '/'], |
| 85 | + $realPath |
| 86 | + ); |
| 87 | + |
| 88 | + $type = strlen($relativePath) < 4 || stripos($relativePath, '.php', -4) === false ? 'directory' : 'file'; |
| 89 | + |
| 90 | + $event->getIO()->write("Improving <info>${relativePath}</info> ${type}..."); |
| 91 | + |
| 92 | + $output = []; |
| 93 | + $return = 0; |
| 94 | + |
| 95 | + // NOTE: workarround: need to run 2 times due to a bug that exits 1 instead of 0 when a file gets fixed |
| 96 | + // https://github.com/squizlabs/PHP_CodeSniffer/issues/1818#issuecomment-735620637 |
| 97 | + exec("${cmd} \"${realPath}\" || ${cmd} \"${realPath}\" -q", $output, $return); |
| 98 | + |
| 99 | + $event->getIO()->write($output, true, IOInterface::VERBOSE); |
| 100 | + |
| 101 | + if ($return !== 0) { |
| 102 | + $event->getIO()->error("Error! Unable to autofix the ${relativePath} file!"); |
| 103 | + $event->getIO()->write( |
| 104 | + '<comment>Run <options=bold>`phpcs`</> manually to check the conflicting files</comment>' |
| 105 | + ); |
| 106 | + exit(1); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + $event->getIO()->write('<info>Everything is awesome!</info>'); |
| 112 | + |
| 113 | + $end_time = microtime(true); |
| 114 | + $execution_time = round($end_time - $start_time, 2); |
| 115 | + |
| 116 | + $event->getIO()->write("Done in ${execution_time}s"); |
| 117 | + |
| 118 | + if (count($ignoredPaths)) { |
| 119 | + $ignoredPaths = array_map(function ($item) { |
| 120 | + return ' - ' . $item; |
| 121 | + }, $ignoredPaths); |
| 122 | + |
| 123 | + $event->getIO()->write('<comment>Note: Some paths were not found:</comment>'); |
| 124 | + $event->getIO()->write($ignoredPaths); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments