Permalink
Browse files

Implement #77 - Do not bail out on first duplicate code unit

  • Loading branch information...
theseer committed Jun 25, 2017
1 parent 0cbb4d1 commit 5ebc9048304608f4224b9db2b16fa1a87d0b478e
View
@@ -40,6 +40,7 @@
* 1 - Execution Error
* 3 - Parameter Error
* 4 - Lint Error
* 5 - Duplicates found Error
*/
define('PHPAB_VERSION', '%development%');
View
@@ -54,6 +54,10 @@ public function run() {
if (!$result->hasUnits()) {
throw new ApplicationException('No units were found - process aborted.', ApplicationException::NoUnitsFound);
}
if ($result->hasDuplicates()) {
return $this->showDuplicatesError($result->getDuplicates());
}
if ($this->config->isCacheEnabled()) {
$this->factory->getCache()->persist($this->config->getCacheFile());
}
@@ -195,6 +199,34 @@ protected function runLint($code) {
return CLI::RC_OK;
}
/**
* @param array $duplicates
*
* @return int
*/
private function showDuplicatesError(array $duplicates) {
$this->logger->log(
sprintf("\nMultiple declarations of trait(s), interface(s) or class(es). Could not generate autoload map.\n"),
STDERR
);
foreach($duplicates as $unit => $files) {
$this->logger->log(
sprintf("\nUnit '%s' defined in:\n", $unit),
STDERR
);
/** @var array $files */
foreach($files as $file) {
$this->logger->log(
sprintf(" - %s\n", $file),
STDERR
);
}
}
return CLI::RC_DUPLICATES_ERROR;
}
}
class ApplicationException extends \Exception {
View
@@ -50,6 +50,7 @@ class CLI {
const RC_EXEC_ERROR = 1;
const RC_PARAM_ERROR = 3;
const RC_LINT_ERROR = 4;
const RC_DUPLICATES_ERROR = 5;
private $pharOption;
private $helpOption;
View
@@ -23,6 +23,11 @@ class CollectorResult {
*/
private $dependencies = array();
/**
* @var array
*/
private $duplicates = array();
public function __construct(array $whitelist, array $blacklist) {
$this->whitelist = $whitelist;
$this->blacklist = $blacklist;
@@ -38,15 +43,11 @@ public function addParseResult(\SplFileInfo $file, ParseResult $result) {
continue;
}
if (isset($this->units[$unit])) {
throw new CollectorResultException(
sprintf(
"Redeclaration of trait, interface or class found:\n\n\tUnit name: %s\n\tFirst occurance: %s\n\tRedeclaration: %s",
$unit,
$this->units[$unit],
$filename
),
CollectorResultException::DuplicateUnitName
);
if (!isset($this->duplicates[$unit])) {
$this->duplicates[$unit] = [ $this->units[$unit] ];
}
$this->duplicates[$unit][] = $filename;
continue;
}
$this->units[$unit] = $filename;
$this->dependencies[$unit] = $result->getDependenciesForUnit($unit);
@@ -57,6 +58,9 @@ public function hasUnits() {
return count($this->units) > 0;
}
public function hasDuplicates() {
return count($this->duplicates) > 0;
}
/**
* @return array
*/
@@ -90,6 +94,10 @@ private function accept($unit) {
return false;
}
public function getDuplicates() {
return $this->duplicates;
}
}
class CollectorResultException extends \Exception {
@@ -0,0 +1,67 @@
<?php
/**
* Copyright (c) 2009-2017 Arne Blankerts <arne@blankerts.de>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of Arne Blankerts nor the names of contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT * NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER ORCONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @package Autoload
* @author Arne Blankerts <arne@blankerts.de>
* @copyright Arne Blankerts <arne@blankerts.de>, All rights reserved.
* @license BSD License
*/
namespace TheSeer\Autoload\Tests {
use TheSeer\Autoload\Config;
use TheSeer\Autoload\Factory;
class DuplicateDetectionTest extends \PHPUnit\Framework\TestCase {
public function testBugIsFixed() {
$config = new Config([]);
$config->setLowercaseMode(true);
$factory = new Factory();
$factory->setConfig($config);
$collector = $factory->getCollector();
$scanner = $factory->getScanner()->getIterator(__DIR__ . '/_data/duplicates');
$collector->processDirectory($scanner);
$result = $collector->getResult();
$this->assertTrue($result->hasDuplicates());
$duplicates = $result->getDuplicates();
$this->assertCount(1, $duplicates);
$this->assertArrayHasKey('a\\b\\c\\duplicate', $duplicates);
$this->assertCount(3, $duplicates['a\\b\\c\\duplicate']);
}
}
}
@@ -0,0 +1,4 @@
<?php
namespace a\b\c;
class Duplicate {}
@@ -0,0 +1,4 @@
<?php
namespace a\b\c;
class Duplicate {}
@@ -0,0 +1,4 @@
<?php
namespace a\b\c;
class Duplicate {}

0 comments on commit 5ebc904

Please sign in to comment.