Skip to content

Commit

Permalink
feat: add php-cs-fixer rules (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyl3x authored Feb 17, 2025
1 parent 7ac8876 commit 871e3c9
Show file tree
Hide file tree
Showing 18 changed files with 100 additions and 96 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ var/
composer.lock
node_modules
.phpunit.cache
/paypal.cache
63 changes: 34 additions & 29 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,49 +13,54 @@
->setRules([
'@Symfony' => true,
'@Symfony:risky' => true,
'linebreak_after_opening_tag' => false,

'blank_line_after_opening_tag' => false,
'phpdoc_summary' => false,
'phpdoc_annotation_without_dot' => false,
'phpdoc_to_comment' => false,
'class_attributes_separation' => ['elements' => ['property' => 'one', 'method' => 'one']],
'concat_space' => ['spacing' => 'one'],
'declare_strict_types' => true,
'strict_comparison' => true,
'strict_param' => true,
'explicit_indirect_variable' => true,
'explicit_string_variable' => true,
'fopen_flags' => false,
'general_phpdoc_annotation_remove' => ['annotations' => ['copyright', 'category']],
'linebreak_after_opening_tag' => false,
'method_argument_space' => ['on_multiline' => 'ensure_fully_multiline'],
'method_chaining_indentation' => true,
'multiline_comment_opening_closing' => true,
'multiline_whitespace_before_semicolons' => true,
'native_function_invocation' => ['scope' => 'namespaced', 'strict' => false, 'exclude' => ['ini_get']],
'no_superfluous_phpdoc_tags' => ['allow_unused_params' => true, 'allow_mixed' => true],
'no_useless_else' => true,
'void_return' => true,
'phpdoc_line_span' => true,
'no_useless_return' => true,
'ordered_class_elements' => true,
'php_unit_dedicate_assert_internal_type' => true,
'php_unit_dedicate_assert' => ['target' => 'newest'],
'php_unit_internal_class' => true,
'php_unit_mock' => true,
'php_unit_test_case_static_method_calls' => ['call_type' => 'static'],
'no_useless_return' => true,
'yoda_style' => [
'equal' => false,
'identical' => false,
'less_and_greater' => false,
],
'single_line_throw' => false,
'fopen_flags' => false,
'self_accessor' => false,
'phpdoc_align' => ['align' => 'left'],
'phpdoc_annotation_without_dot' => false,
'phpdoc_line_span' => true,
'phpdoc_order_by_value' => true,
'phpdoc_order' => ['order' => ['param', 'throws', 'return']],
'class_attributes_separation' => ['elements' => ['property' => 'one', 'method' => 'one']],
'method_argument_space' => ['on_multiline' => 'ensure_fully_multiline'],
'concat_space' => ['spacing' => 'one'],
'native_function_invocation' => [
'scope' => 'namespaced',
'strict' => false,
'exclude' => ['ini_get'],
],
'general_phpdoc_annotation_remove' => ['annotations' => ['copyright', 'category']],
'no_superfluous_phpdoc_tags' => ['allow_unused_params' => true, 'allow_mixed' => true],
'php_unit_dedicate_assert' => ['target' => 'newest'],
'phpdoc_param_order' => true,
'phpdoc_summary' => false,
'phpdoc_to_comment' => false,
'phpdoc_var_annotation_correct_order' => true,
'self_accessor' => false,
'single_line_throw' => false,
'single_quote' => ['strings_containing_single_quote_chars' => true],
'strict_comparison' => true,
'strict_param' => true,
'trailing_comma_in_multiline' => ['after_heredoc' => true, 'elements' => ['array_destructuring', 'arrays', 'match']],
'void_return' => true,
'yoda_style' => ['equal' => false, 'identical' => false, 'less_and_greater' => false],

'header_comment' => ['header' => '(c) shopware AG <info@shopware.com>
For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.', 'separate' => 'bottom', 'location' => 'after_declare_strict', 'comment_type' => 'comment'],
])
->setUsingCache(true)
->setCacheFile(Path::join($_SERVER['SHOPWARE_TOOL_CACHE_ECS'] ?? __DIR__, 'paypal.cache'))
->setCacheFile(Path::join($_SERVER['SHOPWARE_TOOL_CACHE_ECS'] ?? __DIR__, 'var/cache/php-cs-fixer.cache'))
->setFinder(
(new Finder())
->in([__DIR__ . '/src', __DIR__ . '/tests'])
Expand Down
4 changes: 2 additions & 2 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ rules:
- Shopware\Core\DevOps\StaticAnalyze\PHPStan\Rules\Tests\CoversAttributeRule
- Shopware\Core\DevOps\StaticAnalyze\PHPStan\Rules\Tests\MockingSimpleObjectsNotAllowedRule
- Shopware\Core\DevOps\StaticAnalyze\PHPStan\Rules\DecorationPatternRule
- Shopware\Core\DevOps\StaticAnalyze\PHPStan\Rules\AddColumnRule
- Shopware\Core\DevOps\StaticAnalyze\PHPStan\Rules\Migration\AddColumnRule
- Shopware\Core\DevOps\StaticAnalyze\PHPStan\Rules\PackageAnnotationRule
- Shopware\Core\DevOps\StaticAnalyze\PHPStan\Rules\NoAfterStatementRule
- Shopware\Core\DevOps\StaticAnalyze\PHPStan\Rules\Migration\NoAfterStatementRule
- Shopware\Core\DevOps\StaticAnalyze\PHPStan\Rules\NoNewRequestInStorefrontRule

# rules from https://github.com/symplify/phpstan-rules
Expand Down
8 changes: 4 additions & 4 deletions src/Checkout/ExpressCheckout/ExpressCheckoutSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ public function disableAddressValidation(BuildValidationEvent $event): void
}

$event->getDefinition()->set('additionalAddressLine1')
->set('additionalAddressLine2')
->set('phoneNumber');
->set('additionalAddressLine2')
->set('phoneNumber');
}

public function disableCustomerValidation(BuildValidationEvent $event): void
Expand All @@ -211,8 +211,8 @@ public function disableCustomerValidation(BuildValidationEvent $event): void
}

$event->getDefinition()->set('birthdayDay')
->set('birthdayMonth')
->set('birthdayYear');
->set('birthdayMonth')
->set('birthdayYear');
}

public function addPayerIdToCustomer(DataMappingEvent $event): void
Expand Down
34 changes: 17 additions & 17 deletions src/Checkout/Payment/Service/OrderExecuteService.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,6 @@ public function captureOrAuthorizeOrder(
}
}

private function doPayPalRequest(PayPalOrder $paypalOrder, string $salesChannelId, string $partnerAttributionId, string $transactionId, Context $context): PayPalOrder
{
if ($this->checkFinalizedStatus($paypalOrder, $salesChannelId, $transactionId, $context, false)) {
return $paypalOrder;
}

if ($paypalOrder->getIntent() === PaymentIntentV2::CAPTURE) {
$response = $this->orderResource->capture($paypalOrder->getId(), $salesChannelId, $partnerAttributionId);
} else {
$response = $this->orderResource->authorize($paypalOrder->getId(), $salesChannelId, $partnerAttributionId);
}

$this->checkFinalizedStatus($response, $salesChannelId, $transactionId, $context);

return $response;
}

public function checkFinalizedStatus(PayPalOrder $order, string $salesChannelId, string $transactionId, Context $context, bool $refetch = true): bool
{
if ($order->getIntent() === PaymentIntentV2::CAPTURE) {
Expand Down Expand Up @@ -138,6 +121,23 @@ public function checkFinalizedStatus(PayPalOrder $order, string $salesChannelId,
return false;
}

private function doPayPalRequest(PayPalOrder $paypalOrder, string $salesChannelId, string $partnerAttributionId, string $transactionId, Context $context): PayPalOrder
{
if ($this->checkFinalizedStatus($paypalOrder, $salesChannelId, $transactionId, $context, false)) {
return $paypalOrder;
}

if ($paypalOrder->getIntent() === PaymentIntentV2::CAPTURE) {
$response = $this->orderResource->capture($paypalOrder->getId(), $salesChannelId, $partnerAttributionId);
} else {
$response = $this->orderResource->authorize($paypalOrder->getId(), $salesChannelId, $partnerAttributionId);
}

$this->checkFinalizedStatus($response, $salesChannelId, $transactionId, $context);

return $response;
}

private function getPayments(PayPalOrder $order, string $salesChannelId, bool $refetch): ?Payments
{
$payments = $order->getPurchaseUnits()->first()?->getPayments();
Expand Down
10 changes: 5 additions & 5 deletions src/Pos/Api/Common/PosStruct.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function assign(array $arrayData)

if ($this->isScalar($value)) {
if ($value !== null) {
$this->$setterMethod($value);
$this->{$setterMethod}($value);
}

continue;
Expand All @@ -49,7 +49,7 @@ public function assign(array $arrayData)
}

$instance = $this->createNewAssociation($className, $value);
$this->$setterMethod($instance);
$this->{$setterMethod}($instance);

continue;
}
Expand All @@ -64,7 +64,7 @@ static function ($var) {
return $var !== null;
}
);
$this->$setterMethod($arrayData);
$this->{$setterMethod}($arrayData);

continue;
}
Expand All @@ -78,7 +78,7 @@ static function ($var) {
$instance = $this->createNewAssociation($className, $toManyAssociation);
$arrayWithToManyAssociations[] = $instance;
}
$this->$setterMethod($arrayWithToManyAssociations);
$this->{$setterMethod}($arrayWithToManyAssociations);
}

return $this;
Expand All @@ -93,7 +93,7 @@ public function jsonSerialize(): array

foreach (\array_keys(\get_class_vars(static::class)) as $property) {
try {
$data[$property] = $this->$property;
$data[$property] = $this->{$property};
/* @phpstan-ignore-next-line */
} catch (\Error $error) {
$data[$property] = null;
Expand Down
14 changes: 7 additions & 7 deletions src/RestApi/PayPalApiStruct.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,27 @@ public function assign(array $arrayDataWithSnakeCaseKeys): static
}

if ($this->isScalar($value)) {
$this->$setterMethod($value);
$this->{$setterMethod}($value);

continue;
}

/** @var class-string<PayPalApiStruct> $className */
if ($this->isAssociativeArray($value) && $className = $this->getPropertyType($propertyName)) {
$this->$setterMethod((new $className())->assign($value));
$this->{$setterMethod}((new $className())->assign($value));

continue;
}

/** @var class-string<PayPalApiCollection<PayPalApiStruct>> $collectionClass */
if ($collectionClass = $this->getCollection($propertyName)) {
$this->$setterMethod($collectionClass::createFromAssociative($value));
$this->{$setterMethod}($collectionClass::createFromAssociative($value));

continue;
}

// try for scalar value arrays like string[]
$this->$setterMethod($value);
$this->{$setterMethod}($value);
}

return $this;
Expand All @@ -73,7 +73,7 @@ public function jsonSerialize(): array
$snakeCasePropertyName = $nameConverter->normalize($property);

if ((new \ReflectionProperty($this, $property))->isInitialized($this)) {
$data[$snakeCasePropertyName] = $this->$property;
$data[$snakeCasePropertyName] = $this->{$property};
}
}

Expand All @@ -82,12 +82,12 @@ public function jsonSerialize(): array

public function unset(string $propertyName): void
{
unset($this->$propertyName);
unset($this->{$propertyName});
}

public function isset(string $propertyName): bool
{
return isset($this->$propertyName);
return isset($this->{$propertyName});
}

private function isScalar(mixed $value): bool
Expand Down
4 changes: 2 additions & 2 deletions src/Util/IntrospectionProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
#[Package('checkout')]
class IntrospectionProcessor implements ProcessorInterface
{
private Level $level;

private const SKIP_FUNCTIONS = [
'call_user_func',
'call_user_func_array',
];

private Level $level;

/**
* @param string|int|Level $level The minimum logging level at which this Processor will be triggered
*
Expand Down
8 changes: 4 additions & 4 deletions src/Util/LocaleCodeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
#[Package('checkout')]
class LocaleCodeProvider implements ResetInterface
{
private const SUPPORTED_LOCALE_CODE_LENGTH = 5;

private const DEFAULT_LOCALE_CODE = 'en_GB';

private EntityRepository $languageRepository;

private LoggerInterface $logger;
Expand All @@ -28,10 +32,6 @@ class LocaleCodeProvider implements ResetInterface
*/
private array $cache = [];

private const SUPPORTED_LOCALE_CODE_LENGTH = 5;

private const DEFAULT_LOCALE_CODE = 'en_GB';

/**
* @param EntityRepository<LanguageCollection> $languageRepository
*
Expand Down
4 changes: 2 additions & 2 deletions tests/OrdersApi/Builder/APMOrderBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function testGetOrder(string $orderBuilderClass, array $requestData, stri
$paymentSource = $order->getPaymentSource();
static::assertNotNull($paymentSource);
$getter = 'get' . $this->getPropertyName($structClass);
$struct = $paymentSource->$getter();
$struct = $paymentSource->{$getter}();
static::assertInstanceOf($structClass, $struct);
static::assertSame('DE', $struct->getCountryCode());
static::assertSame('Some One', $struct->getName());
Expand Down Expand Up @@ -158,7 +158,7 @@ public function testGetOrderNoShippingAddress(string $orderBuilderClass, array $
$paymentSource = $order->getPaymentSource();
static::assertNotNull($paymentSource);
$getter = 'get' . $this->getPropertyName($structClass);
$struct = $paymentSource->$getter();
$struct = $paymentSource->{$getter}();

static::assertSame(ExperienceContext::SHIPPING_PREFERENCE_NO_SHIPPING, $struct?->getExperienceContext()?->getShippingPreference());
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Pos/Run/PosSyncControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,15 @@ public function testSyncWithInvalidId(string $syncFunction): void
{
$context = Context::createDefaultContext();
$this->expectException(InvalidSalesChannelIdException::class);
$this->posSyncController->$syncFunction(self::INVALID_CHANNEL_ID, $context);
$this->posSyncController->{$syncFunction}(self::INVALID_CHANNEL_ID, $context);
}

#[DataProvider('dataProviderSyncFunctions')]
public function testSyncNormal(string $syncFunction, array $serviceCalls): void
{
$context = Context::createDefaultContext();
$salesChannelId = $this->salesChannelRepoMock->getMockEntity();
$this->posSyncController->$syncFunction($salesChannelId->getId(), $context);
$this->posSyncController->{$syncFunction}($salesChannelId->getId(), $context);

$envelope = \current($this->messageBus->getEnvelopes());
static::assertNotFalse($envelope);
Expand Down
12 changes: 6 additions & 6 deletions tests/Pos/Sync/Inventory/RemoteUpdaterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ public function testUpdateRemoteInventoryVariant(int $localInventory, int $newLo
$bulkChanges->setReturnBalanceForLocationUuid($this->locations['STORE']);

$this->inventoryResource->expects($change === 0 ? static::never() : static::once())
->method('changeInventoryBulk')
->with(static::anything(), $bulkChanges)
->willReturn($this->createStatus($variantChange->getProductUuid(), $variantChange->getVariantUuid()));
->method('changeInventoryBulk')
->with(static::anything(), $bulkChanges)
->willReturn($this->createStatus($variantChange->getProductUuid(), $variantChange->getVariantUuid()));

$this->logger->expects($change === 0 ? static::never() : static::once())
->method('info');
->method('info');

$this->remoteUpdater->updateRemote(new ProductCollection([$product]), $inventoryContext);
}
Expand Down Expand Up @@ -110,8 +110,8 @@ public function testUpdateRemoteInventorySingle(int $localInventory, int $newLoc
$bulkChanges->setReturnBalanceForLocationUuid($this->locations['STORE']);

$this->inventoryResource->expects($change === 0 ? static::never() : static::once())
->method('changeInventoryBulk')
->with(static::anything(), $bulkChanges);
->method('changeInventoryBulk')
->with(static::anything(), $bulkChanges);

$this->remoteUpdater->updateRemote(new ProductCollection([$product]), $inventoryContext);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Pos/Util/LogCleanerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ private function createLogHistory(): RunRepoMock
$runRepository = new RunRepoMock();
for ($i = 1; $i <= 7; ++$i) {
$run = new PosSalesChannelRunEntity();
$run->setId("run$i");
$run->setCreatedAt(new \DateTime("-$i hours"));
$run->setId("run{$i}");
$run->setCreatedAt(new \DateTime("-{$i} hours"));
if ($i === 7) {
$run->setCreatedAt(new \DateTime('-2 months'));
}
Expand Down
4 changes: 2 additions & 2 deletions tests/RestApi/PayPalApiStructStructureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public function testAllStructsHaveSettersAndGetters(string $path): void
static::assertStringContainsString($propertyTypeName ?: '-', $this->getTypeName($reflectionSetterParameters[0]->getType()) ?: '', \sprintf('Setter for property %s in class %s has wrong parameter type', $propertyName, $structClass));

$value = $this->getMockValue($propertyType);
$struct->$setter($value);
static::assertSame($value, $struct->$getter(), \sprintf('Getter for property %s in class %s does not return the same value as set', $propertyName, $structClass));
$struct->{$setter}($value);
static::assertSame($value, $struct->{$getter}(), \sprintf('Getter for property %s in class %s does not return the same value as set', $propertyName, $structClass));
}
}
}
Expand Down
Loading

0 comments on commit 871e3c9

Please sign in to comment.