Skip to content

Commit

Permalink
Merge pull request #2284 from coreshop/coding-standard/refactor-master
Browse files Browse the repository at this point in the history
[CS] Refactor
  • Loading branch information
dpfaffenbauer committed May 19, 2023
2 parents be3c7b9 + 6210e45 commit c072f42
Show file tree
Hide file tree
Showing 23 changed files with 56 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public function supports($request): bool
{
return
$request instanceof Capture &&
$request->getModel() instanceof \ArrayAccess
;
$request->getModel() instanceof \ArrayAccess;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public function supports($request): bool
return
$request instanceof Convert &&
$request->getSource() instanceof PaymentInterface &&
$request->getTo() === 'array'
;
$request->getTo() === 'array';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public function supports($request): bool
{
return
$request instanceof Notify &&
$request->getModel() instanceof \ArrayAccess
;
$request->getModel() instanceof \ArrayAccess;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public function supports($request): bool
{
return
$request instanceof GetStatusInterface &&
$request->getModel() instanceof \ArrayAccess
;
$request->getModel() instanceof \ArrayAccess;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ protected function configure(): void
<<<EOT
The <info>%command.name%</info> executes all Class Patches.
EOT
);
)
;
}

protected function execute(InputInterface $input, OutputInterface $output): int
Expand All @@ -58,8 +59,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$output->writeln(
$this->consoleDiffer->diff(
print_r($this->patcher->old($patch), true),
print_r($this->patcher->new($patch), true)
)
print_r($this->patcher->new($patch), true),
),
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,40 @@ class ColorConsoleDiffFormatter
{
/**
* @var string
*
* @see https://regex101.com/r/ovLMDF/1
*/
private const PLUS_START_REGEX = '#^(\+.*)#';

/**
* @var string
*
* @see https://regex101.com/r/xwywpa/1
*/
private const MINUT_START_REGEX = '#^(\-.*)#';

/**
* @var string
*
* @see https://regex101.com/r/CMlwa8/1
*/
private const AT_START_REGEX = '#^(@.*)#';

/**
* @var string
*
* @see https://regex101.com/r/qduj2O/1
*/
private const NEWLINES_REGEX = "#\n\r|\n#";

private string $template;

public function __construct()
{
public function __construct(
) {
$this->template = sprintf(
'<comment> ---------- begin diff ----------</comment>%s%%s%s<comment> ----------- end diff -----------</comment>' . PHP_EOL,
PHP_EOL,
PHP_EOL
'<comment> ---------- begin diff ----------</comment>%s%%s%s<comment> ----------- end diff -----------</comment>' . \PHP_EOL,
\PHP_EOL,
\PHP_EOL,
);
}

Expand All @@ -68,9 +72,10 @@ private function formatWithTemplate(string $diff, string $template): string
$escapedDiff = OutputFormatter::escape(rtrim($diff));

$escapedDiffLines = array_map(
static function(UnicodeString $string) {
static function (UnicodeString $string) {
return $string->toString();
}, (new UnicodeString($escapedDiff))->split(self::NEWLINES_REGEX)
},
(new UnicodeString($escapedDiff))->split(self::NEWLINES_REGEX),
);

// remove description of added + remove; obvious on diffs
Expand All @@ -96,7 +101,7 @@ static function(UnicodeString $string) {
return $string;
}, $escapedDiffLines);

return sprintf($template, implode(PHP_EOL, $coloredLines));
return sprintf($template, implode(\PHP_EOL, $coloredLines));
}

private function makePlusLinesGreen(string $string): string
Expand All @@ -113,4 +118,4 @@ private function makeAtNoteCyan(string $string): string
{
return (new UnicodeString($string))->replace(self::AT_START_REGEX, '<fg=cyan>$1</fg=cyan>')->toString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class ConsoleDiffer
{
public function __construct(
private Differ $differ,
private ColorConsoleDiffFormatter $colorConsoleDiffFormatter
private ColorConsoleDiffFormatter $colorConsoleDiffFormatter,
) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

namespace CoreShop\Bundle\ClassDefinitionPatchBundle\DependencyInjection;

use CoreShop\Component\Inventory\Checker\AvailabilityChecker;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function load(array $configs, ContainerBuilder $container): void
{
$configs = $this->processConfiguration($this->getConfiguration([], $container), $configs);

$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.yml');

$patches = [];
Expand All @@ -52,13 +52,12 @@ public function load(array $configs, ContainerBuilder $container): void
$field['after'] ?? null,
$field['before'] ?? null,
$field['definition'] ?? null,
])
]),
);

$fields[] = new Reference($fieldId);
}


$patches[$className] = new Definition(Patch::class, [
$className,
$patch['interface'] ?? null,
Expand Down
5 changes: 2 additions & 3 deletions src/CoreShop/Bundle/ClassDefinitionPatchBundle/Patch.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public function __construct(
protected ?string $useTraits,
protected ?string $listingUseTraits,
protected ?array $fields,
)
{
) {
}

public function getClassName(): string
Expand Down Expand Up @@ -81,4 +80,4 @@ public function getFields(): ?array
{
return $this->fields;
}
}
}
5 changes: 2 additions & 3 deletions src/CoreShop/Bundle/ClassDefinitionPatchBundle/PatchField.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ public function __construct(
protected ?string $after,
protected ?string $before,
protected ?array $definition,
)
{
) {
if (null === $this->definition) {
$this->definition = [];
}
Expand All @@ -53,4 +52,4 @@ public function getDefinition(): ?array
{
return $this->definition;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ public function getAfter(): ?string;
public function getBefore(): ?string;

public function getDefinition(): ?array;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ public function getListingUseTraits(): ?string;
* @return PatchField[]|null
*/
public function getFields(): ?array;
}
}
24 changes: 12 additions & 12 deletions src/CoreShop/Bundle/ClassDefinitionPatchBundle/Patcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@

class Patcher implements PatcherInterface
{
public function __construct(protected Patches $patches)
{
public function __construct(
protected Patches $patches,
) {
}

public function getPatches(): array
Expand All @@ -36,11 +37,13 @@ public function patch(): void
foreach ($this->patches->getPatches() as $patch) {
$this->patchClass($patch);
}
}public function old(PatchInterface $patch): array
{
return $this->patchWithClassUpdate($patch)->getOriginalJsonDefinition();
}

public function old(PatchInterface $patch): array
{
return $this->patchWithClassUpdate($patch)->getOriginalJsonDefinition();
}

public function new(PatchInterface $patch): array
{
return $this->patchWithClassUpdate($patch)->getJsonDefinition();
Expand Down Expand Up @@ -116,18 +119,15 @@ protected function patchWithClassUpdate(PatchInterface $patch): ClassUpdate
foreach ($patch->getFields() as $field) {
if ($classUpdater->hasField($field->getFieldName())) {
$classUpdater->replaceField($field->getFieldName(), $field->getDefinition());
}
elseif ($field->getBefore()) {
} elseif ($field->getBefore()) {
$classUpdater->insertFieldBefore($field->getBefore(), $field->getDefinition());
}
elseif ($field->getAfter()) {
} elseif ($field->getAfter()) {
$classUpdater->insertFieldAfter($field->getAfter(), $field->getDefinition());
}
else {
} else {
$classUpdater->insertField($field->getDefinition());
}
}

return $classUpdater;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,15 @@

namespace CoreShop\Bundle\ClassDefinitionPatchBundle;

use CoreShop\Component\Pimcore\DataObject\ClassUpdate;

interface PatcherInterface
{
public function getPatches(): array;

public function old(PatchInterface $patch): array;

public function new(PatchInterface $patch): array;

public function patch(): void;

public function patchClass(Patch $patch): void;
}
}
7 changes: 4 additions & 3 deletions src/CoreShop/Bundle/ClassDefinitionPatchBundle/Patches.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@

class Patches implements PatchesInterface
{
public function __construct(protected array $patches = [])
{
public function __construct(
protected array $patches = [],
) {
}

public function getPatches(): array
Expand All @@ -42,4 +43,4 @@ public function getPatch(string $className)

return $this->patches[$className];
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ public function getPatches(): array;
public function hasPatch(string $className): bool;

public function getPatch(string $className);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
->add('quantity', IntegerType::class, [
'attr' => ['min' => 1],
'label' => 'coreshop.ui.quantity',
])->setDataMapper($this->dataMapper);
])->setDataMapper($this->dataMapper)
;

if ($options['allow_custom_price']) {
$builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ public function supports($request): bool
{
return
$request instanceof Capture &&
$request->getModel() instanceof CoreShopPaymentInterface
;
$request->getModel() instanceof CoreShopPaymentInterface;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ public function supports($request): bool
return
$request instanceof Convert &&
$request->getSource() instanceof PaymentInterface &&
$request->getTo() === 'array'
;
$request->getTo() === 'array';
}

private function formatPrice(int $price): float
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ public function installResources(OutputInterface $output, string $applicationNam
{
$parameter = $applicationName ?
sprintf('%s.dependant.bundles', $applicationName) :
'coreshop.all.dependant.bundles'
;
'coreshop.all.dependant.bundles';

if ($this->kernel->getContainer()->hasParameter($parameter)) {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
abstract class AbstractDefinitionUpdate implements ClassUpdateInterface
{
protected array $jsonDefinition;

protected array $originalJsonDefinition;

protected array $fieldDefinitions;
Expand Down
1 change: 0 additions & 1 deletion src/CoreShop/Component/Pimcore/DataObject/ClassUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

use CoreShop\Component\Pimcore\Exception\ClassDefinitionNotFoundException;
use Pimcore\Model\DataObject;
use Pimcore\Model\DataObject\ClassDefinition;

class ClassUpdate extends AbstractDefinitionUpdate
{
Expand Down

0 comments on commit c072f42

Please sign in to comment.