1 ) { exit( $status ); } } $status = run( 'Deleting the baselines left without entries', __DIR__ . '/generate-baselines.php', array( '--prune' ) ); if ( 0 !== $status ) { exit( $status ); } $exit_code = 0; foreach ( array( PROJECT_CONFIG, STRICT_CONFIG ) as $config ) { $exit_code = max( $exit_code, analyse( "Analyzing with $config", $phpstan, array( "--configuration=$config", ...$args ) ) ); } exit( $exit_code ); } $local_config = $repo_root . '/phpstan.neon'; $strict_config = is_file( $repo_root . '/phpstan-strict.neon' ) ? 'phpstan-strict.neon' : STRICT_CONFIG; // PHPStan reads phpstan.neon in place of phpstan.neon.dist by itself, so the first analysis names neither. $exit_code = analyse( 'Analyzing with ' . ( is_file( $local_config ) ? 'phpstan.neon' : PROJECT_CONFIG ), $phpstan, $args ); if ( is_file( $local_config ) ) { $local_level = get_level( $phpstan, $local_config ); $strict_level = get_level( $phpstan, $repo_root . '/' . $strict_config ); if ( null !== $local_level && null !== $strict_level && is_level_at_least( $local_level, $strict_level ) ) { fwrite( STDERR, "\nSkipping $strict_config: phpstan.neon analyzes at level $local_level, and the strict paths are held to $strict_level. If phpstan.neon leaves any of them out, run `composer phpstan:strict` too.\n" ); exit( $exit_code ); } } exit( max( $exit_code, analyse( "Analyzing the strict paths with $strict_config", $phpstan, array( "--configuration=$strict_config", ...$args ) ) ) ); /** * Runs a command with a heading, passing its output through. * * The heading goes to stderr, so that what the command writes to stdout, such as * a report in a machine-readable format, is left as it is. * * The script runs on the PHP running this one, which Composer's `@php` chose, * rather than on whichever `php` the script's shebang finds first. * * @param string $heading Heading describing the run. * @param string $script Path to the PHP script to run. * @param string[] $args Arguments, which are escaped for the shell here. * @return int Exit status of the script. */ function run( string $heading, string $script, array $args ): int { fwrite( STDERR, "\n== $heading ==\n\n" ); $parts = array( escapeshellarg( PHP_BINARY ), escapeshellarg( $script ) ); foreach ( $args as $arg ) { $parts[] = escapeshellarg( $arg ); } $exit_code = 0; passthru( implode( ' ', $parts ), $exit_code ); return $exit_code; } /** * Runs PHPStan's `analyse` command with a heading, passing its output through. * * @param string $heading Heading describing the run. * @param string $phpstan Path to the PHPStan executable. * @param list $args Arguments to the command, which are escaped for the shell. * @return int Exit status of PHPStan. */ function analyse( string $heading, string $phpstan, array $args ): int { return run( $heading, $phpstan, array( 'analyse', ...$args ) ); } /** * Gets the rule level a configuration analyzes at. * * Only the line of the parameter is matched, rather than decoding the whole dump, * because the JSON PHPStan prints can be invalid: it writes the `\<` in a message * pattern as is, which is not a valid JSON escape. * * @param string $phpstan Path to the PHPStan executable. * @param string $config Absolute path to the configuration file. * @return string|null The rule level, as digits or `max`, or null if it could not be read. */ function get_level( string $phpstan, string $config ): ?string { $output = array(); $exit_code = 0; exec( sprintf( '%s %s dump-parameters --configuration=%s --json 2>/dev/null', escapeshellarg( PHP_BINARY ), escapeshellarg( $phpstan ), escapeshellarg( $config ) ), $output, $exit_code ); // A configuration PHPStan cannot load, for one, leaves no parameters to read. if ( 0 !== $exit_code ) { return null; } foreach ( $output as $line ) { if ( 1 === preg_match( '/^\s*"level":\s*("max"|"?\d+"?),?$/', $line, $matches ) ) { return trim( $matches[1], '"' ); } } return null; } /** * Determines whether a rule level is at least as strict as another. * * `max` is the highest level of whichever PHPStan version is installed, so it is * at least any other. A numbered level is not taken to be at least `max`, since * that would only hold until PHPStan adds a level above it. * * @param string $level Rule level, as digits or `max`. * @param string $minimum Rule level to compare it with, as digits or `max`. * @return bool Whether the level is at least the minimum. */ function is_level_at_least( string $level, string $minimum ): bool { if ( 'max' === $level ) { return true; } if ( 'max' === $minimum ) { return false; } return (int) $level >= (int) $minimum; } /** * Finds an argument that only makes sense for a single analysis. * * Those are a configuration, a level, a report format, a baseline to generate, * `--fix-ignored`, and the paths to analyze. An option's value can follow it as the * next argument, so that is skipped rather than taken for a path. * * @param list $args Arguments. * @return string|null Description of the first such argument, or null if there is none. */ function get_single_analysis_reason( array $args ): ?string { // The options whose value can be given as the next argument rather than after `=`. $takes_value = array( '-c', '--configuration', '-l', '--level', '-a', '--autoload-file', '--error-format', '--memory-limit', '--tmp-file', '--instead-of' ); $single = array( '-c', '--configuration', '-l', '--level', '--error-format', '--generate-baseline', '-b', '--fix-ignored' ); $is_value = false; foreach ( $args as $i => $arg ) { // The value of the option before, given as an argument of its own. if ( $is_value ) { $is_value = false; continue; } if ( '--' === $arg ) { return $i + 1 < count( $args ) ? 'the paths given' : null; } if ( '' === $arg || '-' !== $arg[0] ) { return 'the paths given'; } // A long option's value follows `=`, and a short option's follows its letter directly, as in `-l6`. if ( 0 === strpos( $arg, '--' ) ) { $name = explode( '=', $arg, 2 )[0]; $has_value = false !== strpos( $arg, '=' ); } else { $name = substr( $arg, 0, 2 ); $has_value = strlen( $arg ) > 2; } if ( in_array( $name, $single, true ) ) { return $name; } if ( ! $has_value && in_array( $name, $takes_value, true ) ) { $is_value = true; } } return null; }