Skip to content

Commit

Permalink
Ecs fixes (#409)
Browse files Browse the repository at this point in the history
* Added two custom fixers for ECS
  • Loading branch information
piotrleo authored Dec 15, 2021
1 parent 6a1b664 commit 1520a27
Show file tree
Hide file tree
Showing 117 changed files with 973 additions and 438 deletions.
95 changes: 95 additions & 0 deletions .github/workflows/coding_standard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
name: Coding standard

on:
push:
branches-ignore:
- 'dependabot/**'
pull_request: ~
release:
types: [created]
schedule:
-
cron: "0 1 * * 6" # Run at 1am every Saturday
workflow_dispatch: ~

jobs:
tests:
runs-on: ubuntu-latest

name: "Sylius ${{ matrix.sylius }}, PHP ${{ matrix.php }}, Symfony ${{ matrix.symfony }}, MySQL ${{ matrix.mysql }}"

strategy:
fail-fast: false
matrix:
php: [7.4, 7.3, 8.0]
symfony: [^4.4, ^5.2]
sylius: [~1.8.0, ~1.9.0, ~1.10.0]
node: [10.x]
mysql: [5.7]

exclude:
-
php: 7.3
mysql: 8.0
-
sylius: ~1.8.0
symfony: ^5.2
-
sylius: ~1.8.0
php: 8.0
-
sylius: ~1.9.0
php: 8.0
-
sylius: ~1.10.0
php: 7.3

env:
APP_ENV: test
DATABASE_URL: "mysql://root:[email protected]/sylius?serverVersion=${{ matrix.mysql }}"

steps:
-
uses: actions/checkout@v2
-
name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: "${{ matrix.php }}"
extensions: intl
tools: symfony
coverage: none
-
name: Get Composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

-
name: Cache Composer
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-php-${{ matrix.php }}-composer-${{ hashFiles('**/composer.json **/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-${{ matrix.php }}-composer-
-
name: Restrict Symfony version
if: matrix.symfony != ''
run: |
composer global require --no-progress --no-scripts --no-plugins "symfony/flex:^1.10"
composer config extra.symfony.require "${{ matrix.symfony }}"
-
name: Restrict Sylius version
if: matrix.sylius != ''
run: composer require "sylius/sylius:${{ matrix.sylius }}" --no-update --no-scripts --no-interaction

-
name: Install PHP dependencies
run: composer install --no-interaction

-
name: Run PHPStan
run: vendor/bin/phpstan analyse -c phpstan.neon -l 8 src/

- name: Run ECS
run: vendor/bin/ecs
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"require-dev": {
"behat/behat": "^3.7",
"behat/mink-selenium2-driver": "^1.4",
"bitbag/coding-standard": "dev-main",
"dmore/behat-chrome-extension": "^1.3",
"dmore/chrome-mink-driver": "^2.7",
"friends-of-behat/mink": "^1.8",
Expand Down Expand Up @@ -46,12 +47,11 @@
"symfony/intl": "^4.4 || ^5.2",
"symfony/web-profiler-bundle": "^4.4 || ^5.2",
"vimeo/psalm": "4.7.1",
"matthiasnoback/symfony-config-test": "^4.3",
"polishsymfonycommunity/symfony-mocker-container": "^1.0"
},
"conflict": {
"doctrine/orm": "^2.10"
},
"conflict": {
"doctrine/orm": "^2.10",
"doctrine/dbal": "^3.0",
"symfony/symfony": "4.1.8",
"symfony/browser-kit": "4.1.8",
Expand Down
8 changes: 1 addition & 7 deletions ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,11 @@

declare(strict_types=1);

use PhpCsFixer\Fixer\ArrayNotation\ArraySyntaxFixer;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symplify\EasyCodingStandard\ValueObject\Option;

return static function (ContainerConfigurator $containerConfigurator): void {
$containerConfigurator->import('vendor/sylius-labs/coding-standard/ecs.php');
$services = $containerConfigurator->services();
$services->set(ArraySyntaxFixer::class)
->call('configure', [[
'syntax' => 'short',
]]);
$containerConfigurator->import('vendor/bitbag/coding-standard/ecs.php');

$parameters = $containerConfigurator->parameters();
$parameters->set(Option::PATHS, [
Expand Down
15 changes: 15 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
includes:
- vendor/bitbag/coding-standard/phpstan.neon
parameters:
reportUnmatchedIgnoredErrors: false
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
excludes_analyse:
# Sitemap dependent providers
- 'src/SitemapProvider'
- 'src/Importer/AbstractImporter'
- 'tests/Application/config/bootstrap.php'
- 'tests/Fixture/PageFixtureTest.php'
ignoreErrors:
- '#.*NodeParentInterface.*#'
- '#.*PHPDoc tag @throws with type Exception|Psr\Container\ContainerExceptionInterface is not subtype of Throwable.*#'
2 changes: 1 addition & 1 deletion spec/Assigner/ProductsAssignerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use BitBag\SyliusCmsPlugin\Assigner\ProductsAssignerInterface;
use BitBag\SyliusCmsPlugin\Entity\ProductsAwareInterface;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Repository\ProductRepositoryInterface;

final class ProductsAssignerSpec extends ObjectBehavior
{
Expand Down
2 changes: 1 addition & 1 deletion spec/Assigner/SectionsAssignerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

use BitBag\SyliusCmsPlugin\Assigner\SectionsAssigner;
use BitBag\SyliusCmsPlugin\Assigner\SectionsAssignerInterface;
use BitBag\SyliusCmsPlugin\Entity\SectionableInterface;
use BitBag\SyliusCmsPlugin\Entity\SectionInterface;
use BitBag\SyliusCmsPlugin\Repository\SectionRepositoryInterface;
use PhpSpec\ObjectBehavior;
use BitBag\SyliusCmsPlugin\Entity\SectionableInterface;

final class SectionsAssignerSpec extends ObjectBehavior
{
Expand Down
2 changes: 1 addition & 1 deletion spec/Assigner/TaxonsAssignerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

use BitBag\SyliusCmsPlugin\Assigner\TaxonsAssigner;
use BitBag\SyliusCmsPlugin\Assigner\TaxonsAssignerInterface;
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface;
use BitBag\SyliusCmsPlugin\Entity\TaxonAwareInterface;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\TaxonInterface;
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface;

final class TaxonsAssignerSpec extends ObjectBehavior
{
Expand Down
2 changes: 1 addition & 1 deletion spec/Entity/BlockSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Resource\Model\ResourceInterface;
use Sylius\Component\Core\Model\TaxonInterface;
use Sylius\Component\Resource\Model\ResourceInterface;

final class BlockSpec extends ObjectBehavior
{
Expand Down
1 change: 0 additions & 1 deletion spec/Entity/SectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use BitBag\SyliusCmsPlugin\Entity\Section;
use BitBag\SyliusCmsPlugin\Entity\SectionInterface;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Resource\Model\ResourceInterface;

final class SectionSpec extends ObjectBehavior
Expand Down
2 changes: 1 addition & 1 deletion spec/Twig/Extension/RenderBlockExtensionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ final class RenderBlockExtensionSpec extends ObjectBehavior
{
function let(
RenderBlockRuntimeInterface $blockRuntime
) {
): void {
$this->beConstructedWith($blockRuntime);
}

Expand Down
7 changes: 7 additions & 0 deletions spec/Twig/Extension/RenderProductPagesExtensionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@
namespace spec\BitBag\SyliusCmsPlugin\Twig\Extension;

use BitBag\SyliusCmsPlugin\Twig\Extension\RenderProductPagesExtension;
use BitBag\SyliusCmsPlugin\Twig\Runtime\RenderProductPagesRuntimeInterface;
use PhpSpec\ObjectBehavior;
use Twig\Extension\AbstractExtension;

final class RenderProductPagesExtensionSpec extends ObjectBehavior
{
function let(
RenderProductPagesRuntimeInterface $productPagesRuntime
): void {
$this->beConstructedWith($productPagesRuntime);
}

function it_is_initializable(): void
{
$this->shouldHaveType(RenderProductPagesExtension::class);
Expand Down
3 changes: 2 additions & 1 deletion spec/Twig/Runtime/RenderLinkRuntimeSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use BitBag\SyliusCmsPlugin\Twig\Runtime\RenderLinkRuntimeInterface;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Locale\Context\LocaleContextInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\RouterInterface;
use Twig\Environment;

Expand Down Expand Up @@ -88,6 +89,6 @@ function it_returns_not_found_message_when_getting_link_for_code(
): void {
$localeContext->getLocaleCode()->willReturn("en_US");

$this->getLinkForCode("CODE", ['notFoundMessage' => "message"])->shouldReturn("message");
$this->shouldThrow(NotFoundHttpException::class)->during('getLinkForCode', ["CODE"]);
}
}
4 changes: 2 additions & 2 deletions spec/Twig/Runtime/RenderProductPagesRuntimeSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function it_renders_product_pages(
$channelContext->getChannel()->willReturn($channel);
$page->getSections()->willReturn(new ArrayCollection([$section]));
$section->getCode()->willReturn("SECTION_CODE");
$pageRepository->findByProduct($product, 'WEB')->willReturn([])->shouldBeCalled();
$pageRepository->findByProduct($product, 'WEB', null)->willReturn([])->shouldBeCalled();
$sectionsSorter->sortBySections([])->willReturn([]);
$templatingEngine->render('@BitBagSyliusCmsPlugin/Shop/Product/_pagesBySection.html.twig', ['data' => []])->willReturn('content');

Expand All @@ -81,7 +81,7 @@ function it_renders_product_pages_with_sections(
$channelContext->getChannel()->willReturn($channel);
$page->getSections()->willReturn(new ArrayCollection([$section]));
$section->getCode()->willReturn("SECTION_CODE");
$pageRepository->findByProductAndSectionCode($product, 'SECTION_CODE','WEB')->willReturn([])->shouldBeCalled();
$pageRepository->findByProductAndSectionCode($product, 'SECTION_CODE','WEB', null)->willReturn([])->shouldBeCalled();
$sectionsSorter->sortBySections([])->willReturn([]);
$templatingEngine->render('@BitBagSyliusCmsPlugin/Shop/Product/_pagesBySection.html.twig', ['data' => []])->willReturn('content');

Expand Down
2 changes: 1 addition & 1 deletion src/Assigner/ChannelsAssigner.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct(ChannelRepositoryInterface $channelRepository)
public function assign(ChannelsAwareInterface $channelsAware, array $channelsCodes): void
{
foreach ($channelsCodes as $channelCode) {
/** @var ChannelInterface $channel|null */
/** @var ChannelInterface|null $channel */
$channel = $this->channelRepository->findOneBy(['code' => $channelCode]);

Assert::notNull($channel, sprintf('Channel with %s code not found.', $channelCode));
Expand Down
2 changes: 1 addition & 1 deletion src/Assigner/ProductsAssigner.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct(ProductRepositoryInterface $productRepository)
public function assign(ProductsAwareInterface $productsAware, array $productsCodes): void
{
foreach ($productsCodes as $productCode) {
/** @var ProductInterface $product */
/** @var ProductInterface|null $product */
$product = $this->productRepository->findOneBy(['code' => $productCode]);

Assert::notNull($product, sprintf('Product with %s code not found.', $productCode));
Expand Down
2 changes: 1 addition & 1 deletion src/Assigner/SectionsAssigner.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct(SectionRepositoryInterface $sectionRepository)
public function assign(SectionableInterface $sectionsAware, array $sectionsCodes): void
{
foreach ($sectionsCodes as $sectionCode) {
/** @var SectionInterface $section */
/** @var SectionInterface|null $section */
$section = $this->sectionRepository->findOneBy(['code' => $sectionCode]);

Assert::notNull($section, sprintf('Section with %s code not found.', $sectionCode));
Expand Down
2 changes: 1 addition & 1 deletion src/Assigner/TaxonsAssigner.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct(TaxonRepositoryInterface $taxonRepository)
public function assign(TaxonAwareInterface $taxonAware, array $taxonCodes): void
{
foreach ($taxonCodes as $taxonCode) {
/** @var TaxonInterface $taxon */
/** @var TaxonInterface|null $taxon */
$taxon = $this->taxonRepository->findOneBy(['code' => $taxonCode]);

Assert::notNull($taxon, sprintf('Taxon with %s code not found.', $taxonCode));
Expand Down
4 changes: 3 additions & 1 deletion src/Command/ImportFromCsvCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ protected function configure(): void
;
}

protected function execute(InputInterface $input, OutputInterface $output): void
protected function execute(InputInterface $input, OutputInterface $output): int
{
$resourceName = $input->getArgument('resource');
$file = $input->getArgument('file');

$this->importProcessor->process($resourceName, $file);

return 0;
}
}
8 changes: 4 additions & 4 deletions src/Controller/Action/Admin/UploadEditorImageAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct(

public function __invoke(Request $request): Response
{
/** @var UploadedFile $image */
/** @var UploadedFile|null $image */
$image = $request->files->get('upload');

if (null === $image || !$this->isValidImage($image)) {
Expand All @@ -62,7 +62,7 @@ public function __invoke(Request $request): Response

private function isValidImage(UploadedFile $image): bool
{
return in_array($image->getMimeType(), ['image/jpeg', 'image/png', 'image/gif', 'image/svg+xml']);
return in_array($image->getMimeType(), ['image/jpeg', 'image/png', 'image/gif', 'image/svg+xml'], true);
}

private function createMedia(UploadedFile $image): MediaInterface
Expand All @@ -87,12 +87,12 @@ private function createMediaCode(string $name): string
$i = 0;

do {
if ($i > 0) {
if (0 < $i) {
$code = $code . '_image_' . (string) $i;
}

++$i;
} while (count($this->mediaRepository->findBy(['code' => $code])) > 0);
} while (0 < count($this->mediaRepository->findBy(['code' => $code])));

return $code;
}
Expand Down
7 changes: 7 additions & 0 deletions src/Controller/BlockController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
namespace BitBag\SyliusCmsPlugin\Controller;

use BitBag\SyliusCmsPlugin\Entity\BlockInterface;
use BitBag\SyliusCmsPlugin\Resolver\BlockResourceResolverInterface;
use FOS\RestBundle\View\View;
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
use Sylius\Component\Resource\ResourceActions;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Webmozart\Assert\Assert;

final class BlockController extends ResourceController
{
Expand All @@ -28,6 +30,7 @@ public function renderBlockAction(Request $request): Response
$this->isGrantedOr403($configuration, ResourceActions::SHOW);

$code = $request->get('code');
/** @var BlockResourceResolverInterface $blockResourceResolver */
$blockResourceResolver = $this->get('bitbag_sylius_cms_plugin.resolver.block_resource');
$block = $blockResourceResolver->findOrLog($code);

Expand All @@ -38,6 +41,8 @@ public function renderBlockAction(Request $request): Response
$this->eventDispatcher->dispatch(ResourceActions::SHOW, $configuration, $block);

if (!$configuration->isHtmlRequest()) {
Assert::true(null !== $this->viewHandler);

return $this->viewHandler->handle($configuration, View::create($block));
}

Expand Down Expand Up @@ -70,6 +75,8 @@ public function previewAction(Request $request): Response
$block->setCurrentLocale($request->get('_locale', $defaultLocale));

if (!$configuration->isHtmlRequest()) {
Assert::true(null !== $this->viewHandler);

return $this->viewHandler->handle($configuration, View::create($block));
}

Expand Down
Loading

0 comments on commit 1520a27

Please sign in to comment.