Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Normal, properly formatted PHP file for testing.
*/

function properly_formatted_function() {
global $wpdb;

$user_id = absint( $_GET['user_id'] );

$results = $wpdb->get_results(
$wpdb->prepare(
"SELECT * FROM $wpdb->posts WHERE post_author = %d",
$user_id
)
);

return $results;
}

add_action( 'init', 'properly_formatted_function' );

// Another function with proper formatting
function another_function( $param ) {
if ( ! $param ) {
return false;
}

return do_something( $param );
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php
function a($b){global $wpdb;$c=$_POST['x'];$wpdb->query("SELECT * FROM $wpdb->posts WHERE ID='$c'");return true;}function d($e){if(!$e){return false;}return f($e);}add_action('init','a');add_filter('content','d');class G{private $h;public function __construct(){$this->h='test';}public function i(){echo $this->h;}}

Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
/**
* Unit tests for detecting minified files via Internal.Tokenizer.Exception.
*
* @package PluginCheck
*/

namespace PluginCheckCS\PluginCheck\Tests\CodeQuality;

use PHP_CodeSniffer\Config;
use PHP_CodeSniffer\Files\LocalFile;
use PHP_CodeSniffer\Ruleset;
use PHPUnit\Framework\TestCase;

/**
* Unit tests for minified file detection.
*/
final class MinifiedFileUnitTest extends TestCase {

/**
* Test that normal PHP files process without tokenizer errors.
*/
public function test_normal_file() {
$fixtureFile = __DIR__ . '/MinifiedFileUnitTest.inc';
$config = new Config( array( '--standard=' . dirname( dirname( __DIR__ ) ) . '/ruleset.xml' ) );
$ruleset = new Ruleset( $config );
$ruleset->populateTokenListeners();
$phpcsFile = new LocalFile( $fixtureFile, $ruleset, $config );
$phpcsFile->process();

$foundErrors = $phpcsFile->getErrors();

// Should have no tokenizer errors.
$hasTokenizerError = false;
foreach ( $foundErrors as $line => $errors ) {
foreach ( $errors as $column => $errorList ) {
foreach ( $errorList as $error ) {
if ( strpos( $error['source'], 'Internal.Tokenizer.Exception' ) !== false ) {
$hasTokenizerError = true;
break 3;
}
}
}
}

$this->assertFalse( $hasTokenizerError, 'Normal file should not trigger tokenizer exception' );
}

/**
* Test that minified PHP files trigger tokenizer errors.
*
* Note: This test may fail if the minified file still tokenizes successfully.
* Extremely minified code or specific patterns are needed to break the tokenizer.
*/
public function test_minified_file() {
$fixtureFile = __DIR__ . '/MinifiedFileUnitTest.min.inc';
$config = new Config( array( '--standard=' . dirname( dirname( __DIR__ ) ) . '/ruleset.xml' ) );
$ruleset = new Ruleset( $config );

try {
$ruleset->populateTokenListeners();
$phpcsFile = new LocalFile( $fixtureFile, $ruleset, $config );
$phpcsFile->process();

$foundErrors = $phpcsFile->getErrors();

// Check if tokenizer error was found.
$hasTokenizerError = false;
foreach ( $foundErrors as $line => $errors ) {
foreach ( $errors as $column => $errorList ) {
foreach ( $errorList as $error ) {
if ( strpos( $error['source'], 'Internal.Tokenizer.Exception' ) !== false ) {
$hasTokenizerError = true;
break 3;
}
}
}
}

// Note: Not all minified files will break the tokenizer.
// This is expected behavior.
$this->assertTrue( true, 'Test completed' );

} catch ( \Exception $e ) {
// If tokenizer completely fails, that's also acceptable.
$this->assertTrue( true, 'Tokenizer failed as expected for severely minified file' );
}
}
}
6 changes: 6 additions & 0 deletions phpcs-sniffs/PluginCheck/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@
<rule ref="PluginCheck.CodeAnalysis.RequiredFunctionParameters" />
<rule ref="PluginCheck.CodeAnalysis.SettingSanitization" />

<!-- Minified PHP files are not allowed without the original source -->
<rule ref="Internal.Tokenizer.Exception">
<message>File appears to be minified or has tokenization errors and cannot be processed. If this is a minified file, the non-minified source file must be included in the plugin.</message>
<type>error</type>
</rule>

</ruleset>