[ ...] */ function argus_should_keep_comment( string $text, bool $is_first_token_in_bootstrap_file ): bool { if ( $is_first_token_in_bootstrap_file && false !== strpos( $text, 'Plugin Name:' ) ) { return true; } $trimmed = ltrim( $text, "/ \t" ); return 0 === stripos( $trimmed, 'phpcs:' ); } function argus_strip_file( string $path ): void { $source = file_get_contents( $path ); $tokens = token_get_all( $source ); $is_bootstrap = ( 'argus-wordpress-defence.php' === basename( $path ) ); $out = ''; $seen_first_comment = false; foreach ( $tokens as $token ) { if ( is_array( $token ) ) { list( $id, $text ) = $token; if ( T_COMMENT === $id || T_DOC_COMMENT === $id ) { $is_first = $is_bootstrap && ! $seen_first_comment; $seen_first_comment = true; if ( argus_should_keep_comment( $text, $is_first ) ) { $out .= $text; } // else: drop the comment text entirely (whitespace/newlines // around it are separate whitespace tokens, left untouched). continue; } $out .= $text; } else { $out .= $token; } } // Collapse runs of 3+ blank lines left behind by removed comment // blocks down to a single blank line, purely cosmetic. $out = preg_replace( "/\n{3,}/", "\n\n", $out ); file_put_contents( $path, $out ); } function argus_collect_php_files( array $paths ): array { $files = array(); foreach ( $paths as $p ) { if ( is_dir( $p ) ) { $it = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $p, FilesystemIterator::SKIP_DOTS ) ); foreach ( $it as $f ) { if ( 'php' === strtolower( $f->getExtension() ) ) { $files[] = $f->getPathname(); } } } elseif ( is_file( $p ) ) { $files[] = $p; } } return $files; } $args = array_slice( $argv, 1 ); if ( empty( $args ) ) { fwrite( STDERR, "Usage: php bin/strip-dev-comments.php [...]\n" ); exit( 1 ); } $files = argus_collect_php_files( $args ); $count = 0; foreach ( $files as $file ) { argus_strip_file( $file ); $count++; } echo "Stripped development comments from {$count} PHP files.\n";