Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolve #189: Add feature for unused zombies
Signed-off-by: Andreas Frömer <andreas.froemer@check24.de>
- Loading branch information
1 parent
7c63986
commit c1ee7e8
Showing
19 changed files
with
412 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ComposerUnused\ComposerUnused\Filter; | ||
|
||
use ArrayIterator; | ||
use Countable; | ||
use IteratorAggregate; | ||
|
||
/** | ||
* @implements IteratorAggregate<FilterInterface> | ||
*/ | ||
final class FilterCollection implements IteratorAggregate, Countable | ||
{ | ||
/** | ||
* List of global named packages which should be excluded from unused check | ||
* Key => package name | ||
* Value => always "used" | ||
*/ | ||
private const GLOBAL_NAMED_EXCLUSION = [ | ||
'composer-plugin-api' => true | ||
]; | ||
|
||
/** | ||
* List of global pattern which should be excluded from unused check | ||
* Key => pattern | ||
* Value => always "used" | ||
*/ | ||
private const GLOBAL_PATTERN_EXCLUSION = [ | ||
'/-implementation$/i' => true | ||
]; | ||
|
||
/** @var array<FilterInterface> */ | ||
private array $items; | ||
|
||
/** | ||
* @param array<string> $namedFilter | ||
* @param array<string> $patternFilter | ||
*/ | ||
public function __construct(array $namedFilter, array $patternFilter) | ||
{ | ||
$globalNamedFilter = array_map( | ||
static fn(string $named, $used) => new NamedFilter($named, $used), | ||
array_keys(self::GLOBAL_NAMED_EXCLUSION), | ||
array_values(self::GLOBAL_NAMED_EXCLUSION) | ||
); | ||
|
||
$globalPatternFilter = | ||
array_map( | ||
static fn(string $pattern, $used) => new PatternFilter($pattern, $used), | ||
array_keys(self::GLOBAL_PATTERN_EXCLUSION), | ||
array_values(self::GLOBAL_PATTERN_EXCLUSION) | ||
); | ||
|
||
$named = array_map(static fn(string $named) => new NamedFilter($named), $namedFilter); | ||
$pattern = array_map(static fn(string $pattern) => new PatternFilter($pattern), $patternFilter); | ||
|
||
$this->items = array_merge($globalNamedFilter, $globalPatternFilter, $named, $pattern); | ||
} | ||
|
||
/** | ||
* @return ArrayIterator<int, FilterInterface> | ||
*/ | ||
public function getIterator(): ArrayIterator | ||
{ | ||
return new ArrayIterator($this->items); | ||
} | ||
|
||
public function count(): int | ||
{ | ||
return count($this->items); | ||
} | ||
|
||
/** | ||
* @return array<FilterInterface> | ||
*/ | ||
public function getUnused(): array | ||
{ | ||
return array_filter( | ||
$this->items, | ||
static fn(FilterInterface $filter) => $filter->used() === false | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ComposerUnused\ComposerUnused\Filter; | ||
|
||
use ComposerUnused\ComposerUnused\Dependency\DependencyInterface; | ||
|
||
interface FilterInterface | ||
{ | ||
public function applies(DependencyInterface $dependency): bool; | ||
|
||
public function used(): bool; | ||
|
||
public function toString(): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ComposerUnused\ComposerUnused\Filter; | ||
|
||
use ComposerUnused\ComposerUnused\Dependency\DependencyInterface; | ||
|
||
final class NamedFilter implements FilterInterface | ||
{ | ||
private string $filterString; | ||
private bool $used = false; | ||
private bool $alwaysUsed; | ||
|
||
public function __construct(string $filterString, bool $alwaysUsed = false) | ||
{ | ||
$this->filterString = $filterString; | ||
$this->alwaysUsed = $alwaysUsed; | ||
} | ||
|
||
public function applies(DependencyInterface $dependency): bool | ||
{ | ||
return $this->used = $dependency->getName() === $this->filterString; | ||
} | ||
|
||
public function used(): bool | ||
{ | ||
return $this->alwaysUsed || $this->used; | ||
} | ||
|
||
public function toString(): string | ||
{ | ||
return $this->filterString; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ComposerUnused\ComposerUnused\Filter; | ||
|
||
use ComposerUnused\ComposerUnused\Dependency\DependencyInterface; | ||
|
||
final class PatternFilter implements FilterInterface | ||
{ | ||
private string $pattern; | ||
private bool $used = false; | ||
private bool $alwaysUsed; | ||
|
||
public function __construct(string $pattern, bool $alwaysUsed = false) | ||
{ | ||
$this->pattern = $pattern; | ||
$this->alwaysUsed = $alwaysUsed; | ||
} | ||
|
||
public function applies(DependencyInterface $dependency): bool | ||
{ | ||
return $this->used = (bool)preg_match($this->pattern, $dependency->getName()); | ||
} | ||
|
||
public function used(): bool | ||
{ | ||
return $this->alwaysUsed || $this->used; | ||
} | ||
|
||
public function toString(): string | ||
{ | ||
return $this->pattern; | ||
} | ||
} |
Oops, something went wrong.