Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 37 |
n/a |
0 / 0 |
CRAP | n/a |
0 / 0 |
||
| 1 | <?php |
| 2 | /** |
| 3 | * This is a phpcs bootstrap file to apply the filter's Config and RuleSet modifications |
| 4 | * when phpcs is passed a file via stdin. |
| 5 | * |
| 6 | * @package automattic/jetpack-phpcs-filter |
| 7 | */ |
| 8 | |
| 9 | use PHP_CodeSniffer\Autoload; |
| 10 | use PHP_CodeSniffer\Util; |
| 11 | |
| 12 | call_user_func( |
| 13 | function () { |
| 14 | global $runner; |
| 15 | |
| 16 | $config = $runner->config; |
| 17 | if ( ! $config->stdin || ! $config->filter ) { |
| 18 | return; |
| 19 | } |
| 20 | |
| 21 | // phpcs has two ways to determine the path for stdin. The newer, documented way is the `--stdin-path=$path` command line option. |
| 22 | // The older (and not really documented) way is to prepend a line "phpcs_input_file:$path" to the input on stdin itself. |
| 23 | // Fortunately it's possible to get that stdin without screwing up phpcs's own use of it. |
| 24 | $path = null; |
| 25 | if ( $config->stdinPath ) { |
| 26 | $path = realpath( $config->stdinPath ); |
| 27 | } else { |
| 28 | // This should match the logic in phpcs's Runner::run(). |
| 29 | $fileContents = $config->stdinContent; |
| 30 | if ( $fileContents === null ) { |
| 31 | $handle = fopen( 'php://stdin', 'r' ); |
| 32 | stream_set_blocking( $handle, true ); |
| 33 | $fileContents = stream_get_contents( $handle ); |
| 34 | fclose( $handle ); |
| 35 | // Save the stdin contents so phpcs's Runner::run() will find it later. |
| 36 | $config->stdinContent = $fileContents; |
| 37 | } |
| 38 | |
| 39 | // This should match the logic in phpcs's DummyFile::__construct(). |
| 40 | if ( substr( $fileContents, 0, 17 ) === 'phpcs_input_file:' ) { |
| 41 | try { |
| 42 | $eolChar = Util\Common::detectLineEndings( $fileContents ); |
| 43 | } catch ( RuntimeException $e ) { |
| 44 | return; |
| 45 | } |
| 46 | $eolPos = strpos( $fileContents, $eolChar ); |
| 47 | $path = trim( substr( $fileContents, 17, ( $eolPos - 17 ) ) ); |
| 48 | } |
| 49 | } |
| 50 | if ( ! $path ) { |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | // This should match the logic in phpcs's FileList::getFilterClass(). |
| 55 | if ( strpos( $config->filter, '.' ) !== false ) { |
| 56 | // This is a path to a custom filter class. |
| 57 | $filename = realpath( $config->filter ); |
| 58 | if ( false === $filename ) { |
| 59 | return; |
| 60 | } |
| 61 | $filterClass = Autoload::loadFile( $filename ); |
| 62 | } else { |
| 63 | $filterClass = '\\PHP_CodeSniffer\\Filters\\' . $config->filter; |
| 64 | } |
| 65 | |
| 66 | // Ask the filter which config and ruleset it should use for the path. |
| 67 | $iter = new RecursiveArrayIterator( array( $path ) ); |
| 68 | $filter = new $filterClass( $iter, '/', $config, $runner->ruleset ); |
| 69 | if ( is_callable( array( $filter, 'getConfigAndRuleset' ) ) ) { |
| 70 | try { |
| 71 | list( $runner->config, $runner->ruleset ) = $filter->getConfigAndRuleset( $path ); |
| 72 | } catch ( Exception $ex ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch |
| 73 | // Don't care. |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | ); |