Skip to content

Commit

Permalink
refactor: Use friendsofphp/php-cs-fixer instead of squizlabs/php_code…
Browse files Browse the repository at this point in the history
…sniffer (#22)
  • Loading branch information
roadiz-ci committed Nov 6, 2024
1 parent 470ae6a commit 4597672
Show file tree
Hide file tree
Showing 134 changed files with 952 additions and 2,402 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/run-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,5 @@ jobs:
${{ runner.os }}-php-${{ matrix.php-version }}-
- name: Install Dependencies
run: composer install --no-scripts --no-ansi --no-interaction --no-progress
- name: Run PHP Code Sniffer
run: vendor/bin/phpcs -p ./src
- name: Run PHPStan
run: vendor/bin/phpstan analyse --no-progress -c phpstan.neon
5 changes: 0 additions & 5 deletions Makefile

This file was deleted.

3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@
"phpstan/phpstan-doctrine": "^1.3",
"roadiz/entity-generator": "2.4.x-dev",
"roadiz/jwt": "2.4.x-dev",
"roadiz/random": "2.4.x-dev",
"squizlabs/php_codesniffer": "^3.5"
"roadiz/random": "2.4.x-dev"
},
"autoload": {
"psr-4": {
Expand Down
26 changes: 0 additions & 26 deletions phpcs.xml.dist

This file was deleted.

24 changes: 8 additions & 16 deletions src/AjaxControllers/AbstractAjaxController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
abstract class AbstractAjaxController extends RozierApp
{
public function __construct(
protected readonly SerializerInterface $serializer
protected readonly SerializerInterface $serializer,
) {
}

Expand All @@ -46,11 +46,7 @@ protected function getTranslation(Request $request): ?TranslationInterface
}

/**
* @param Request $request
* @param string $method
* @param bool $requestCsrfToken
*
* @return bool Return true if request is valid, else throw exception
* @return bool Return true if request is valid, else throw exception
*/
protected function validateRequest(Request $request, string $method = 'POST', bool $requestCsrfToken = true): bool
{
Expand All @@ -59,15 +55,15 @@ protected function validateRequest(Request $request, string $method = 'POST', bo
}

if (
$requestCsrfToken === true &&
!$this->isCsrfTokenValid(static::AJAX_TOKEN_INTENTION, $request->get('_token'))
true === $requestCsrfToken
&& !$this->isCsrfTokenValid(static::AJAX_TOKEN_INTENTION, $request->get('_token'))
) {
throw new BadRequestHttpException('Bad CSRF token');
}

if (
in_array(\mb_strtolower($method), static::$validMethods) &&
\mb_strtolower($request->getMethod()) != \mb_strtolower($method)
in_array(\mb_strtolower($method), static::$validMethods)
&& \mb_strtolower($request->getMethod()) != \mb_strtolower($method)
) {
throw new BadRequestHttpException('Bad method');
}
Expand All @@ -84,18 +80,14 @@ protected function sortIsh(array &$arr, array $map): array
if ($element == $value->getId()) {
$return[] = $value;
unset($arr[$key]);
break 1;
break;
}
}
}

return $return;
}

/**
* @param array $data
* @return JsonResponse
*/
protected function createSerializedResponse(array $data): JsonResponse
{
return new JsonResponse(
Expand All @@ -105,7 +97,7 @@ protected function createSerializedResponse(array $data): JsonResponse
SerializationContext::create()->setGroups([
'document_display',
'explorer_thumbnail',
'model'
'model',
])
),
200,
Expand Down
23 changes: 7 additions & 16 deletions src/AjaxControllers/AjaxAbstractFieldsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ abstract class AjaxAbstractFieldsController extends AbstractAjaxController
{
public function __construct(
protected readonly HandlerFactoryInterface $handlerFactory,
SerializerInterface $serializer
SerializerInterface $serializer,
) {
parent::__construct($serializer);
}
Expand All @@ -28,21 +28,16 @@ protected function findEntity(int|string $entityId): ?AbstractField

/**
* Handle actions for any abstract fields.
*
* @param Request $request
* @param AbstractField|null $field
*
* @return null|Response
*/
protected function handleFieldActions(Request $request, AbstractField $field = null): ?Response
protected function handleFieldActions(Request $request, ?AbstractField $field = null): ?Response
{
$this->validateRequest($request);

if ($field !== null) {
if (null !== $field) {
/*
* Get the right update method against "_action" parameter
*/
if ($request->get('_action') !== 'updatePosition') {
if ('updatePosition' !== $request->get('_action')) {
throw new BadRequestHttpException('Action does not exist');
}

Expand All @@ -57,13 +52,7 @@ protected function handleFieldActions(Request $request, AbstractField $field = n
return null;
}

/**
* @param array $parameters
* @param AbstractField|null $field
*
* @return array
*/
protected function updatePosition(array $parameters, AbstractField $field = null): array
protected function updatePosition(array $parameters, ?AbstractField $field = null): array
{
if (!empty($parameters['afterFieldId']) && is_numeric($parameters['afterFieldId'])) {
$afterField = $this->findEntity((int) $parameters['afterFieldId']);
Expand All @@ -76,6 +65,7 @@ protected function updatePosition(array $parameters, AbstractField $field = null
$handler = $this->handlerFactory->getHandler($field);
$handler->cleanPositions();
$this->em()->flush();

return [
'statusCode' => '200',
'status' => 'success',
Expand All @@ -95,6 +85,7 @@ protected function updatePosition(array $parameters, AbstractField $field = null
$handler = $this->handlerFactory->getHandler($field);
$handler->cleanPositions();
$this->em()->flush();

return [
'statusCode' => '200',
'status' => 'success',
Expand Down
14 changes: 4 additions & 10 deletions src/AjaxControllers/AjaxAttributeValuesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ final class AjaxAttributeValuesController extends AbstractAjaxController
* Handle AJAX edition requests for NodeTypeFields
* such as coming from widgets.
*
* @param Request $request
* @param int $attributeValueId
*
* @return Response JSON response
*/
public function editAction(Request $request, int $attributeValueId): Response
Expand All @@ -36,13 +33,8 @@ public function editAction(Request $request, int $attributeValueId): Response
/** @var AttributeValue|null $attributeValue */
$attributeValue = $this->em()->find(AttributeValue::class, (int) $attributeValueId);

if ($attributeValue === null) {
throw $this->createNotFoundException($this->getTranslator()->trans(
'attribute_value.%attributeValueId%.not_exists',
[
'%attributeValueId%' => $attributeValueId
]
));
if (null === $attributeValue) {
throw $this->createNotFoundException($this->getTranslator()->trans('attribute_value.%attributeValueId%.not_exists', ['%attributeValueId%' => $attributeValueId]));
}

$this->denyAccessUnlessGranted(NodeVoter::EDIT_ATTRIBUTE, $attributeValue->getAttributable());
Expand Down Expand Up @@ -79,6 +71,7 @@ protected function updatePosition(array $parameters, AttributeValue $attributeVa
}
$attributeValue->setPosition($afterAttributeValue->getPosition() + 0.5);
$this->em()->flush();

return [
'statusCode' => '200',
'status' => 'success',
Expand All @@ -96,6 +89,7 @@ protected function updatePosition(array $parameters, AttributeValue $attributeVa
}
$attributeValue->setPosition($beforeAttributeValue->getPosition() - 0.5);
$this->em()->flush();

return [
'statusCode' => '200',
'status' => 'success',
Expand Down
10 changes: 1 addition & 9 deletions src/AjaxControllers/AjaxCustomFormFieldsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ final class AjaxCustomFormFieldsController extends AjaxAbstractFieldsController
* Handle AJAX edition requests for CustomFormFields
* such as coming from widgets.
*
* @param Request $request
* @param int $customFormFieldId
*
* @return Response JSON response
*/
public function editAction(Request $request, int $customFormFieldId): Response
Expand All @@ -30,12 +27,7 @@ public function editAction(Request $request, int $customFormFieldId): Response
return $response;
}

throw $this->createNotFoundException($this->getTranslator()->trans(
'field.%customFormFieldId%.not_exists',
[
'%customFormFieldId%' => $customFormFieldId
]
));
throw $this->createNotFoundException($this->getTranslator()->trans('field.%customFormFieldId%.not_exists', ['%customFormFieldId%' => $customFormFieldId]));
}

protected function getEntityClass(): string
Expand Down
11 changes: 3 additions & 8 deletions src/AjaxControllers/AjaxCustomFormsExplorerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ final class AjaxCustomFormsExplorerController extends AbstractAjaxController
{
public function __construct(
private readonly ExplorerItemFactoryInterface $explorerItemFactory,
SerializerInterface $serializer
SerializerInterface $serializer,
) {
parent::__construct($serializer);
}

/**
* @param Request $request
*
* @return Response JSON response
*/
public function indexAction(Request $request): Response
Expand Down Expand Up @@ -60,8 +58,6 @@ public function indexAction(Request $request): Response
/**
* Get a CustomForm list from an array of id.
*
* @param Request $request
* @return Response
* @throws NotSupported
*/
public function listAction(Request $request): Response
Expand All @@ -73,7 +69,7 @@ public function listAction(Request $request): Response
$this->denyAccessUnlessGranted('ROLE_ACCESS_CUSTOMFORMS');

$cleanCustomFormsIds = array_filter($request->query->filter('ids', [], \FILTER_DEFAULT, [
'flags' => \FILTER_FORCE_ARRAY
'flags' => \FILTER_FORCE_ARRAY,
]));
$customFormsArray = [];

Expand All @@ -91,15 +87,14 @@ public function listAction(Request $request): Response
return $this->createSerializedResponse([
'status' => 'confirm',
'statusCode' => 200,
'forms' => $customFormsArray
'forms' => $customFormsArray,
]);
}

/**
* Normalize response CustomForm list result.
*
* @param iterable<CustomForm> $customForms
* @return array
*/
private function normalizeCustomForms(iterable $customForms): array
{
Expand Down
18 changes: 6 additions & 12 deletions src/AjaxControllers/AjaxDocumentsExplorerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class AjaxDocumentsExplorerController extends AbstractAjaxController
{
public function __construct(
private readonly ExplorerItemFactoryInterface $explorerItemFactory,
SerializerInterface $serializer
SerializerInterface $serializer,
) {
parent::__construct($serializer);
}
Expand Down Expand Up @@ -47,7 +47,7 @@ public function indexAction(Request $request): JsonResponse
Document::class,
$arrayFilter,
[
'createdAt' => 'DESC'
'createdAt' => 'DESC',
]
);
$listManager->setDisplayingNotPublishedNodes(true);
Expand All @@ -69,7 +69,7 @@ public function indexAction(Request $request): JsonResponse

if ($request->query->has('folderId') && $request->get('folderId') > 0) {
$responseArray['filters'] = array_merge($responseArray['filters'], [
'folderId' => $request->get('folderId')
'folderId' => $request->get('folderId'),
]);
}

Expand All @@ -80,9 +80,6 @@ public function indexAction(Request $request): JsonResponse

/**
* Get a Document list from an array of id.
*
* @param Request $request
* @return JsonResponse
*/
public function listAction(Request $request): JsonResponse
{
Expand All @@ -92,7 +89,7 @@ public function listAction(Request $request): JsonResponse
throw new InvalidParameterException('Ids should be provided within an array');
}
$cleanDocumentIds = array_filter($request->query->filter('ids', [], \FILTER_DEFAULT, [
'flags' => \FILTER_FORCE_ARRAY
'flags' => \FILTER_FORCE_ARRAY,
]));
$documentsArray = [];

Expand All @@ -111,15 +108,14 @@ public function listAction(Request $request): JsonResponse
'status' => 'confirm',
'statusCode' => 200,
'documents' => $documentsArray,
'trans' => $this->getTrans()
'trans' => $this->getTrans(),
]);
}

/**
* Normalize response Document list result.
*
* @param iterable<Document> $documents
* @return array
*/
private function normalizeDocuments(iterable $documents): array
{
Expand All @@ -135,16 +131,14 @@ private function normalizeDocuments(iterable $documents): array

/**
* Get an array of translations.
*
* @return array
*/
private function getTrans(): array
{
return [
'editDocument' => $this->getTranslator()->trans('edit.document'),
'unlinkDocument' => $this->getTranslator()->trans('unlink.document'),
'linkDocument' => $this->getTranslator()->trans('link.document'),
'moreItems' => $this->getTranslator()->trans('more.documents')
'moreItems' => $this->getTranslator()->trans('more.documents'),
];
}
}
Loading

0 comments on commit 4597672

Please sign in to comment.