Skip to content

Commit

Permalink
Fix fatal 'class already declared' errors when using external standards
Browse files Browse the repository at this point in the history
I've seen similar issues now in several external standards, where people are receiving a `Cannot declare class Standard\Sniffs\Category\SomethingSniff, because the name is already in use` error.

I've not been able to reproduce the issues, but noticed that - at least in some of the cases - this involved external sniffs which extend other external sniffs and use a `use` statement for the parent sniff.

I suspect that the fact that I cannot reproduce this, may be due to sniffs not loading in a predefined order, but depending on when the directive to include the sniff/standard is encountered in the ruleset.

I imagine that if a child sniff is included first and a parent sniff second, this causes the fatal error.

Using `include_once` instead of `include` for typical class based files should solve this problem.

To that end, I've reviewed all uses of `include/require` in PHPCS and the three addressed in this PR are the result of that review.
  • Loading branch information
jrfnl committed Mar 6, 2018
1 parent e1d3a5b commit 7130abe
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
4 changes: 2 additions & 2 deletions autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static function load($class)
if (strpos(__DIR__, 'phar://') !== 0
&& file_exists(__DIR__.'/../../autoload.php') === true
) {
self::$composerAutoloader = include __DIR__.'/../../autoload.php';
self::$composerAutoloader = include_once __DIR__.'/../../autoload.php';
if (self::$composerAutoloader instanceof \Composer\Autoload\ClassLoader) {
self::$composerAutoloader->unregister();
self::$composerAutoloader->register();
Expand Down Expand Up @@ -164,7 +164,7 @@ public static function loadFile($path)
$interfaces = get_declared_interfaces();
$traits = get_declared_traits();

include $path;
include_once $path;

$className = null;
$newClasses = array_reverse(array_diff(get_declared_classes(), $classes));
Expand Down
2 changes: 1 addition & 1 deletion src/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ private function run()

// Include bootstrap files.
foreach ($this->config->bootstrap as $bootstrap) {
include $bootstrap;
include_once $bootstrap;
}

if ($this->config->stdin === true) {
Expand Down

0 comments on commit 7130abe

Please sign in to comment.