Skip to content

Commit 7130abe

Browse files
committed
Fix fatal 'class already declared' errors when using external standards
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.
1 parent e1d3a5b commit 7130abe

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

autoload.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public static function load($class)
7676
if (strpos(__DIR__, 'phar://') !== 0
7777
&& file_exists(__DIR__.'/../../autoload.php') === true
7878
) {
79-
self::$composerAutoloader = include __DIR__.'/../../autoload.php';
79+
self::$composerAutoloader = include_once __DIR__.'/../../autoload.php';
8080
if (self::$composerAutoloader instanceof \Composer\Autoload\ClassLoader) {
8181
self::$composerAutoloader->unregister();
8282
self::$composerAutoloader->register();
@@ -164,7 +164,7 @@ public static function loadFile($path)
164164
$interfaces = get_declared_interfaces();
165165
$traits = get_declared_traits();
166166

167-
include $path;
167+
include_once $path;
168168

169169
$className = null;
170170
$newClasses = array_reverse(array_diff(get_declared_classes(), $classes));

src/Runner.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ private function run()
313313

314314
// Include bootstrap files.
315315
foreach ($this->config->bootstrap as $bootstrap) {
316-
include $bootstrap;
316+
include_once $bootstrap;
317317
}
318318

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

0 commit comments

Comments
 (0)