Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PHPStan and enable it in CI #257

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ jobs:
name: Validate database schema
run: (cd tests/Application && bin/console doctrine:schema:validate)

# -
# name: Run PHPStan
# run: bin/phpstan analyse -c phpstan.neon -l max src/
-
name: Run PHPStan
run: bin/phpstan analyse -c phpstan.neon

# -
# name: Run PHPSpec
Expand Down
13 changes: 7 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"license": "MIT",
"require": {
"php": "^7.3",
"sylius/sylius": "~1.8.0 || ~1.9.0",
"portphp/portphp": "^1.2",
"symfony/stopwatch": "^4.4 || ^5.2",
"queue-interop/queue-interop": "^0.6.2 || ^0.7 || ^0.8"
"queue-interop/queue-interop": "^0.6.2 || ^0.7 || ^0.8",
"sylius/sylius": "~1.8.0 || ~1.9.0",
"symfony/stopwatch": "^4.4 || ^5.2"
},
"suggest": {
"portphp/spreadsheet": "To support importing Excel and LibreOffice Calc files, use version ^1.1",
Expand All @@ -32,10 +32,11 @@
"lakion/mink-debug-extension": "^1.2.3",
"phpspec/phpspec": "^7.0",
"phpstan/extension-installer": "^1.0",
"phpstan/phpstan": "0.12.82",
"phpstan/phpstan-doctrine": "0.12.33",
"phpstan/phpstan": "^0.12.82",
"phpstan/phpstan-doctrine": "^0.12.33",
"phpstan/phpstan-strict-rules": "^0.12.0",
"phpstan/phpstan-webmozart-assert": "0.12.12",
"phpstan/phpstan-symfony": "^0.12.0",
"phpstan/phpstan-webmozart-assert": "^0.12.12",
"phpunit/phpunit": "^9.5",
"sensiolabs/security-checker": "^6.0",
"sylius-labs/coding-standard": "^3.2",
Expand Down
6 changes: 6 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
parameters:
level: max
paths:
- src
reportUnmatchedIgnoredErrors: false
checkMissingIterableValueType: false

symfony:
console_application_loader: src/FOSSyliusImportExportPlugin.php
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ondrejmirtes do you understand the error ? This seems related to phpstan/phpstan-symfony#46

Fatal error: Uncaught Error: Call to a member function all() on int in /home/ubuntu/projects/SyliusImportExportPlugin/vendor/phpstan/phpstan-symfony/src/Symfony/ConsoleApplicationResolver.php:56

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you're loading this file: https://github.com/FriendsOfSylius/SyliusImportExportPlugin/blob/master/src/FOSSyliusImportExportPlugin.php which isn't a script returning Application instance but just a file with a class that doesn't return anything.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you're loading this file: https://github.com/FriendsOfSylius/SyliusImportExportPlugin/blob/master/src/FOSSyliusImportExportPlugin.php which isn't a script returning Application instance but just a file with a class that doesn't return anything.

In case of library, how should this be handled please ?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need a file that looks like this:

<?php

$app = new Application();
$app->add(new MyCommand());

return $app;


excludes_analyse:
# Makes PHPStan crash
- 'src/DependencyInjection/Configuration.php'
Expand Down
6 changes: 3 additions & 3 deletions src/Command/ExportDataCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
/** @var string $exporter */
$exporter = $input->getArgument('exporter');

if (empty($exporter)) {
if ('' === $exporter) {
$this->listExporters($input, $output);

return 0;
Expand Down Expand Up @@ -121,7 +121,7 @@ private function listExporters(InputInterface $input, OutputInterface $output, ?

$list = [];
foreach ($exporters as $exporter => $formats) {
// prints the exporterentity, implodes the types and outputs them in a form
// prints the exporter entity, implodes the types and outputs them in a form
$list[] = sprintf(
'%s (formats: %s)',
$exporter,
Expand All @@ -132,7 +132,7 @@ private function listExporters(InputInterface $input, OutputInterface $output, ?
$io = new SymfonyStyle($input, $output);
$io->listing($list);

if ($errorMessage) {
if (null !== $errorMessage) {
throw new \RuntimeException($errorMessage);
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/Command/ExportDataToMessageQueueCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ protected function configure(): void
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output): void
protected function execute(InputInterface $input, OutputInterface $output): int
{
/** @var string $exporter */
$exporter = $input->getArgument('exporter');

if (empty($exporter)) {
if ('' === $exporter) {
$this->listExporters($input, $output);

return;
return 0;
}

$domain = 'sylius';
Expand Down Expand Up @@ -82,6 +82,8 @@ protected function execute(InputInterface $input, OutputInterface $output): void
$mqItemWriter = $this->container->get('sylius.message_queue_writer');
$this->export($mqItemWriter, $name, $idsToExport, $exporter);
$this->finishExport($items, 'message queue', $name, $output);

return 0;
}

private function listExporters(InputInterface $input, OutputInterface $output, ?string $errorMessage = null): void
Expand Down Expand Up @@ -110,7 +112,7 @@ private function listExporters(InputInterface $input, OutputInterface $output, ?
$io = new SymfonyStyle($input, $output);
$io->listing($list);

if ($errorMessage) {
if (null !== $errorMessage) {
throw new \RuntimeException($errorMessage);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Command/ImportDataCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
/** @var string $importer */
$importer = $input->getArgument('importer');
if (empty($importer)) {
if ('' === $importer) {
$this->listImporters($input, $output);

return 0;
Expand Down Expand Up @@ -104,7 +104,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$io = new SymfonyStyle($input, $output);

$details = $input->getOption('details');
if ($details) {
if ('' !== $details) {
$imported = implode(', ', $result->getSuccessRows());
$skipped = implode(', ', $result->getSkippedRows());
$failed = implode(', ', $result->getFailedRows());
Expand Down Expand Up @@ -153,7 +153,7 @@ private function listImporters(InputInterface $input, OutputInterface $output, ?
$io = new SymfonyStyle($input, $output);
$io->listing($list);

if ($errorMessage) {
if (null !== $errorMessage) {
throw new \RuntimeException($errorMessage);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Command/ImportDataFromMessageQueueCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
/** @var string $importer */
$importer = $input->getArgument('importer');

if (empty($importer)) {
if ('' === $importer) {
$this->listImporters($input, $output);

return 0;
Expand Down Expand Up @@ -92,7 +92,7 @@ private function finishImport(string $name, OutputInterface $output): void
$output->writeln($message);
}

private function importJsonDataFromMessageQueue(ItemReaderInterface $mqItemReader, $importer, SingleDataArrayImporterInterface $service, OutputInterface $output, int $timeout): void
private function importJsonDataFromMessageQueue(ItemReaderInterface $mqItemReader, string $importer, SingleDataArrayImporterInterface $service, OutputInterface $output, int $timeout): void
{
$mqItemReader->initQueue('sylius.export.queue.' . $importer);
$mqItemReader->readAndImport($service, $timeout);
Expand Down Expand Up @@ -124,7 +124,7 @@ private function listImporters(InputInterface $input, OutputInterface $output, ?
$io = new SymfonyStyle($input, $output);
$io->listing($list);

if ($errorMessage) {
if (null !== $errorMessage) {
throw new \RuntimeException($errorMessage);
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/Controller/ExportDataController.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,13 @@ private function getResourcesItems(ResourceGridView $resources): array

for ($i = 0; $i < $data->getNbPages(); ++$i) {
$data->setCurrentPage($i + 1);
$results = array_merge($results, iterator_to_array($data->getCurrentPageResults()));
$results = array_merge($results, (array) $data->getCurrentPageResults());
}

return $results;
}

return [];
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/ImportDataController.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function importAction(Request $request): RedirectResponse
}
$referer = $request->headers->get('referer');

return new RedirectResponse($referer);
return new RedirectResponse($referer ?? '');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm really not sure about this one

}

private function getForm(string $importerType): FormInterface
Expand Down
4 changes: 2 additions & 2 deletions src/DependencyInjection/Compiler/RegisterExporterPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ public function process(ContainerBuilder $container): void
$name = ExporterRegistry::buildServiceName($type, $format);
$exportersRegistry->addMethodCall('register', [$name, new Reference($id)]);

if ($container->getParameter('sylius.exporter.web_ui')) {
if (null !== $container->getParameter('sylius.exporter.web_ui')) {
$this->registerTypeAndFormat($type, $format);
}
}

if ($container->getParameter('sylius.exporter.web_ui') && !empty($this->typesAndFormats)) {
if (null !== $container->getParameter('sylius.exporter.web_ui') && (null !== $this->typesAndFormats && '' != $this->typesAndFormats)) {
$this->registerEventListenersForExportButton($container);
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/DependencyInjection/Compiler/RegisterImporterPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@

final class RegisterImporterPass implements CompilerPassInterface
{
/** @var string[] */
private $eventHookNames = [
'taxonomy' => 'app.block_event_listener.admin.taxon.create.after_content',
];

/** @var string[] */
private $eventNames = [
'taxonomy' => 'sonata.block.event.sylius.admin.taxon.create.after_content',
];

/** @var string[] */
private $templateNames = [
'taxonomy' => '@FOSSyliusImportExportPlugin/Taxonomy/import.html.twig',
];
Expand Down Expand Up @@ -50,7 +53,7 @@ public function process(ContainerBuilder $container): void

$importersRegistry->addMethodCall('register', [$name, new Reference($id)]);

if ($container->getParameter('sylius.importer.web_ui')) {
if (null !== $container->getParameter('sylius.importer.web_ui')) {
$this->registerImportFormBlockEvent($container, $type);
}
}
Expand Down
10 changes: 9 additions & 1 deletion src/DependencyInjection/ParameterBag/ParameterBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@

final class ParameterBag extends FrozenParameterBag implements ParameterBagInterface
{
/** @var Container */
private $container;

public function __construct(Container $container)
public function __construct(Container $container, array $parameters = [])
{
parent::__construct($parameters);
$this->container = $container;
}

/**
* {@inheritdoc}
*
* @return string[]
*/
public function all()
{
Expand All @@ -33,6 +37,8 @@ public function all()

/**
* {@inheritdoc}
*
* @param string $name
*/
public function get($name)
{
Expand All @@ -41,6 +47,8 @@ public function get($name)

/**
* {@inheritdoc}
*
* @param string $name
*/
public function has($name)
{
Expand Down
17 changes: 3 additions & 14 deletions src/Exporter/JsonResourceExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,12 @@

final class JsonResourceExporter extends ResourceExporter
{
/** @var array[] */
private $data = [];

/** @var string */
private $filename;

/**
* @param string[] $resourceKeys
*/
public function __construct(
PluginPoolInterface $pluginPool,
array $resourceKeys,
?TransformerPoolInterface $transformerPool
) {
$this->pluginPool = $pluginPool;
$this->transformerPool = $transformerPool;
$this->resourceKeys = $resourceKeys;
}

/**
* {@inheritdoc}
*/
Expand All @@ -48,7 +36,8 @@ public function export(array $idsToExport): void
*/
public function getExportedData(): string
{
return json_encode($this->data) ?: '';
$data = json_encode($this->data);
return $data !== false ? $data : '';
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Exporter/MqItemWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function write(array $items): void
{
foreach ($items as $item) {
$message = $this->context->createMessage(
json_encode($item) ?: '',
false !== json_encode($item) ? json_encode($item) : '',
[],
['recordedOn' => (new \DateTime())->format('Y-m-d H:i:s')]
);
Expand Down
5 changes: 3 additions & 2 deletions src/Exporter/ORM/Hydrator/OrderHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;
use Sylius\Component\Core\Model\Order;
use Sylius\Component\Resource\Model\ResourceInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;

Expand Down Expand Up @@ -46,7 +47,7 @@ public function getHydratedResources(array $idsToExport): array
private function findOrdersQb(array $idsToExport): QueryBuilder
{
/**
* @var \Doctrine\ORM\EntityRepository
* @var \Doctrine\ORM\EntityRepository<Order>
*/
$repository = $this->repository;

Expand All @@ -62,7 +63,7 @@ private function findOrdersQb(array $idsToExport): QueryBuilder
private function hydrateOrderItemsQb(array $idsToExport): QueryBuilder
{
/**
* @var \Doctrine\ORM\EntityRepository
* @var \Doctrine\ORM\EntityRepository<Order>
*/
$repository = $this->repository;

Expand Down
2 changes: 1 addition & 1 deletion src/Exporter/Plugin/OrderResourcePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ private function addOrderItemData(array $items, OrderInterface $resource): void
$str = '';

foreach ($items as $itemId => $item) {
if (!empty($str)) {
if (null != $str && '' != $str) {
$str .= ' | ';
}
$str .= sprintf('%dx %s(id:%d)', $item['count'], $item['name'], $itemId);
Expand Down
6 changes: 3 additions & 3 deletions src/Exporter/Plugin/PluginPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function getDataForId(string $id): array
$result = $this->getDataForIdFromPlugin($id, $plugin, $result);
}

if (!empty($this->exportKeysNotFound)) {
if (null != $this->exportKeysNotFound && '' != $this->exportKeysNotFound) {
throw new \InvalidArgumentException(sprintf(
'Not all defined export keys have been found: "%s". Choose from: "%s"',
implode(', ', $this->exportKeysNotFound),
Expand All @@ -80,14 +80,14 @@ public function getDataForId(string $id): array
private function getDataForIdFromPlugin(string $id, PluginInterface $plugin, array $result): array
{
foreach ($plugin->getData($id, $this->exportKeys) as $exportKey => $exportValue) {
if (false === empty($result[$exportKey])) {
if (false === ([] === $result[$exportKey])) {
continue;
}

// no other plugin has delivered a value till now
$result[$exportKey] = $exportValue;

$foundKey = array_search($exportKey, $this->exportKeysNotFound);
$foundKey = array_search($exportKey, $this->exportKeysNotFound, true);
unset($this->exportKeysNotFound[$foundKey]);
}

Expand Down
Loading