replace double foreach with a filter and reduce

This commit is contained in:
danilopolani
2022-03-07 17:51:39 +01:00
parent 985bbf4ea5
commit edd1d890ca

View File

@ -155,19 +155,20 @@ final class TestCaseFactory
$methods $methods
)); ));
$classAttributes = []; $classAttributesCode = [];
$classAvailableAttributes = array_filter(self::$attributes, fn (string $attribute) => $attribute::ABOVE_CLASS);
foreach (self::$attributes as $attribute) { foreach ($classAvailableAttributes as $attribute) {
if ($attribute::ABOVE_CLASS) { $classAttributesCode = array_reduce(
foreach ($methods as $methodFactory) { $methods,
$classAttributes = (new $attribute())->__invoke($methodFactory, $classAttributes); fn (array $carry, TestCaseMethodFactory $methodFactory) => (new $attribute())->__invoke($methodFactory, $carry),
} $classAttributesCode
} );
} }
$classAttributes = implode('', array_map( $classAttributesCode = implode('', array_map(
static fn (string $attribute) => sprintf("\n %s", $attribute), static fn (string $attribute) => sprintf("\n %s", $attribute),
array_unique($classAttributes), array_unique($classAttributesCode),
)); ));
try { try {
@ -177,7 +178,7 @@ final class TestCaseFactory
use Pest\Repositories\DatasetsRepository as __PestDatasets; use Pest\Repositories\DatasetsRepository as __PestDatasets;
use Pest\TestSuite as __PestTestSuite; use Pest\TestSuite as __PestTestSuite;
$classAttributes $classAttributesCode
final class $className extends $baseClass implements $hasPrintableTestCaseClassFQN { final class $className extends $baseClass implements $hasPrintableTestCaseClassFQN {
$traitsCode $traitsCode