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 errors for tests #6961

Merged
merged 7 commits into from
Mar 26, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
14 changes: 12 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@
"matthiasnoback/symfony-config-test": "^4.2",
"matthiasnoback/symfony-dependency-injection-test": "^4.1",
"phpstan/extension-installer": "^1.0",
"phpstan/phpstan": "^0.12.29",
"phpstan/phpstan-symfony": "^0.12.10",
"phpstan/phpstan": "^0.12.82",
"phpstan/phpstan-phpunit": "^0.12.18",
"phpstan/phpstan-symfony": "^0.12.21",
"psalm/plugin-phpunit": "^0.15.1",
"psalm/plugin-symfony": "^2.0",
"psr/event-dispatcher": "^1.0",
"sonata-project/intl-bundle": "^2.4",
Expand Down Expand Up @@ -107,5 +109,13 @@
"psr-4": {
"Sonata\\AdminBundle\\Tests\\": "tests/"
}
},
"scripts": {
"post-install-cmd": [
"[ $COMPOSER_DEV_MODE -eq 0 ] || vendor/bin/simple-phpunit install"
],
"post-update-cmd": [
"[ $COMPOSER_DEV_MODE -eq 0 ] || vendor/bin/simple-phpunit install"
]
}
}
14 changes: 11 additions & 3 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
parameters:
level: 5
bootstrapFiles:
- vendor/bin/.phpunit/phpunit/vendor/autoload.php
paths:
- src
excludes_analyse:
# temporarily ignore template files
- src/Resources/**.tpl.php
- tests
excludePaths:
analyseAndScan:
# temporarily ignore template files
- src/Resources/**.tpl.php
# BC-layer is not supported by phpstan https://github.com/phpstan/phpstan/issues/3542
- tests/Fixtures/StubTranslator.php
ignoreErrors:
- '#Trying to mock an undefined method [a-zA-Z]*\(\) on class stdClass\.#'
VincentLanglet marked this conversation as resolved.
Show resolved Hide resolved
treatPhpDocTypesAsCertain: false
checkGenericClassInNonGenericObjectType: true
checkMissingIterableValueType: true
Expand Down
7 changes: 6 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<psalm xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://getpsalm.org/schema/config" errorLevel="6" resolveFromConfigFile="true" xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd" errorBaseline="psalm-baseline.xml">
<psalm xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://getpsalm.org/schema/config" errorLevel="6" resolveFromConfigFile="true" xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd" autoloader="vendor/bin/.phpunit/phpunit/vendor/autoload.php" errorBaseline="psalm-baseline.xml">
<projectFiles>
<directory name="src"/>
<directory name="tests"/>
<ignoreFiles>
<directory name="src/Resources/skeleton"/>
<directory name="vendor"/>
</ignoreFiles>
</projectFiles>
<plugins>
<pluginClass class="Psalm\SymfonyPsalmPlugin\Plugin"/>
<pluginClass class="Psalm\PhpUnitPlugin\Plugin"/>
</plugins>
</psalm>
2 changes: 0 additions & 2 deletions src/Admin/AdminInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,6 @@ public function setSubClasses(array $subClasses): void;

/**
* Returns true if the admin has the sub classes.
*
* @phpstan-param class-string<T> $name
*/
public function hasSubClass(string $name): bool;

Expand Down
12 changes: 10 additions & 2 deletions src/Admin/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
* item_adds: array,
* items: array<array-key, array{
* admin?: string,
* label?: string,
* label: string,
* roles: list<string>,
* route?: string,
* route: string,
* router_absolute: bool,
* route_params: array<string, string>
* }>,
Expand Down Expand Up @@ -67,6 +67,14 @@ final class Pool
*/
private $adminClasses = [];

/**
* @param string[] $adminServices
* @param array<string, array<string, mixed>> $adminGroups
* @param array<class-string, string[]> $adminClasses
*
* @phpstan-param array<string, array<string, mixed>> $adminGroups
* @psalm-param array<string, Group> $adminGroups
*/
public function __construct(
ContainerInterface $container,
array $adminServices = [],
Expand Down
2 changes: 1 addition & 1 deletion src/Command/GenerateObjectAclCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function execute(InputInterface $input, OutputInterface $output): int

$securityIdentity = null;
if ($input->getOption('step') && $this->askConfirmation($input, $output, "<question>Set an object owner?</question>\n", 'no')) {
$username = $this->askAndValidate($input, $output, 'Please enter the username: ', '', 'Sonata\AdminBundle\Command\Validators::validateUsername');
$username = $this->askAndValidate($input, $output, 'Please enter the username: ', '', [Validators::class, 'validateUsername']);

$securityIdentity = new UserSecurityIdentity($username, $this->getUserModelClass($input, $output));
}
Expand Down
7 changes: 0 additions & 7 deletions src/Datagrid/ProxyQueryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@
*/
interface ProxyQueryInterface
{
/**
* @param mixed[] $args
*
* @return mixed
*/
public function __call(string $name, array $args);

/**
* @return mixed
*/
Expand Down
2 changes: 1 addition & 1 deletion src/FieldDescription/FieldDescriptionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public function hasAdmin(): bool;
/**
* Merge option values related to the provided option name.
*
* @param array<string, mixed> $options
* @param mixed[] $options
*
* @throws \RuntimeException
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Filter/FilterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ final class FilterFactory implements FilterFactoryInterface

/**
* @param array<string, string> $types
*
* @psalm-suppress ContainerDependency
*/
public function __construct(ContainerInterface $container, array $types = [])
{
Expand Down
7 changes: 6 additions & 1 deletion src/Filter/FilterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public function setLabel($label): void;
*/
public function getDefaultOptions(): array;

/**
* @return array<string, mixed>
*/
public function getOptions(): array;

/**
* @param mixed $default
*
Expand All @@ -80,7 +85,7 @@ public function initialize(string $name, array $options = []): void;
public function getFieldName(): string;

/**
* @return array<string, string> array of mappings
* @return array<array<string, mixed>> array of mappings
*/
public function getParentAssociationMappings(): array;

Expand Down
10 changes: 5 additions & 5 deletions src/Form/DataTransformer/ModelToIdPropertyTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,13 @@ public function reverseTransform($value)
}

/**
* @param object|object[]|null $value
* @param object|object[]|\ArrayAccess<string|int, object>|null $value
*
* @throws \InvalidArgumentException
*
* @return array<string|int, int|string|array<string>>
*
* @phpstan-param T|T[]|null $value
* @phpstan-param T|T[]|\ArrayAccess<string|int, T>|null $value
*/
public function transform($value)
{
Expand All @@ -138,8 +138,8 @@ public function transform($value)
return $result;
}

$isArray = \is_array($value);
if ($this->multiple) {
$isArray = \is_array($value);
if (!$isArray && substr(\get_class($value), -1 * \strlen($this->className)) === $this->className) {
throw new \InvalidArgumentException(
'A multiple selection must be passed a collection not a single value.'
Expand All @@ -157,9 +157,9 @@ public function transform($value)
);
}
} else {
if (substr(\get_class($value), -1 * \strlen($this->className)) === $this->className) {
if (!$isArray && substr(\get_class($value), -1 * \strlen($this->className)) === $this->className) {
$collection = [$value];
} elseif ($value instanceof \ArrayAccess) {
} elseif ($isArray || ($value instanceof \ArrayAccess)) {
throw new \InvalidArgumentException(
'A single selection must be passed a single value not a collection.'
.' Make sure that form option "multiple=false" is set for many-to-one relation and "multiple=true"'
Expand Down
3 changes: 3 additions & 0 deletions src/Model/AuditManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ final class AuditManager implements AuditManagerInterface
*/
private $container;

/**
* @psalm-suppress ContainerDependency
*/
public function __construct(ContainerInterface $container)
{
$this->container = $container;
Expand Down
2 changes: 2 additions & 0 deletions src/Route/AdminPoolLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ final class AdminPoolLoader extends Loader

/**
* @param string[] $adminServiceIds
*
* @psalm-suppress ContainerDependency
*/
public function __construct(Pool $pool, array $adminServiceIds, ContainerInterface $container)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Route/RouteCollectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ interface RouteCollectionInterface
* @param array<string, mixed> $defaults
* @param array<string, string> $requirements
* @param array<string, mixed> $options
* @param array<string, string> $schemes
* @param array<string, string> $methods
* @param string[] $schemes
* @param string[] $methods
*
* @return static
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Twig/Extension/RenderElementExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ final class RenderElementExtension extends AbstractExtension

/**
* @internal This class should only be used through Twig
*
* @psalm-suppress ContainerDependency
*/
public function __construct(
PropertyAccessorInterface $propertyAccessor,
Expand Down
3 changes: 3 additions & 0 deletions src/Twig/Extension/TemplateRegistryExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ final class TemplateRegistryExtension extends AbstractExtension
*/
private $container;

/**
* @psalm-suppress ContainerDependency
*/
public function __construct(TemplateRegistryInterface $globalTemplateRegistry, ContainerInterface $container)
{
$this->globalTemplateRegistry = $globalTemplateRegistry;
Expand Down
4 changes: 3 additions & 1 deletion src/Util/FormViewIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ public function next(): void

public function key(): string
{
return $this->current()->vars['id'];
$current = $this->current();

return $current->vars['id'];
}

public function valid(): bool
Expand Down
14 changes: 7 additions & 7 deletions tests/Action/AppendFormFieldElementActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

namespace Sonata\AdminBundle\Tests\Action;

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Sonata\AdminBundle\Action\AppendFormFieldElementAction;
use Sonata\AdminBundle\Action\GetShortObjectDescriptionAction;
use Sonata\AdminBundle\Admin\AdminHelper;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Admin\Pool;
Expand All @@ -37,34 +37,34 @@ final class AppendFormFieldElementActionTest extends TestCase
private $pool;

/**
* @var Environment
* @var Environment&MockObject
*/
private $twig;

/**
* @var GetShortObjectDescriptionAction
* @var AppendFormFieldElementAction
*/
private $action;

/**
* @var AdminInterface
* @var AdminInterface&MockObject
*/
private $admin;

/**
* @var AdminHelper
* @var AdminHelper&MockObject
*/
private $helper;

protected function setUp(): void
{
$this->twig = $this->createStub(Environment::class);
$this->twig = $this->createMock(Environment::class);
$this->admin = $this->createMock(AdminInterface::class);
$this->admin->expects($this->once())->method('setRequest');
$container = new Container();
$container->set('sonata.post.admin', $this->admin);
$this->pool = new Pool($container, ['sonata.post.admin']);
$this->helper = $this->createStub(AdminHelper::class);
$this->helper = $this->createMock(AdminHelper::class);
$this->action = new AppendFormFieldElementAction(
$this->twig,
$this->pool,
Expand Down
3 changes: 2 additions & 1 deletion tests/Action/DashboardActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Sonata\AdminBundle\Tests\Action;

use PHPUnit\Framework\MockObject\Stub;
use PHPUnit\Framework\TestCase;
use Sonata\AdminBundle\Action\DashboardAction;
use Sonata\AdminBundle\Admin\BreadcrumbsBuilderInterface;
Expand All @@ -26,7 +27,7 @@
class DashboardActionTest extends TestCase
{
/**
* @var MutableTemplateRegistryInterface
* @var MutableTemplateRegistryInterface&Stub
*/
private $templateRegistry;

Expand Down
3 changes: 2 additions & 1 deletion tests/Action/GetShortObjectDescriptionActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Sonata\AdminBundle\Tests\Action;

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Sonata\AdminBundle\Action\GetShortObjectDescriptionAction;
use Sonata\AdminBundle\Admin\AdminInterface;
Expand Down Expand Up @@ -42,7 +43,7 @@ final class GetShortObjectDescriptionActionTest extends TestCase
private $action;

/**
* @var AdminInterface
* @var AdminInterface&MockObject
*/
private $admin;

Expand Down
11 changes: 5 additions & 6 deletions tests/Action/RetrieveAutocompleteItemsActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@

namespace Sonata\AdminBundle\Tests\Action;

use PHPUnit\Framework\MockObject\Stub;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Sonata\AdminBundle\Action\GetShortObjectDescriptionAction;
use Sonata\AdminBundle\Action\RetrieveAutocompleteItemsAction;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Admin\Pool;
Expand All @@ -39,12 +38,12 @@ final class RetrieveAutocompleteItemsActionTest extends TestCase
private $pool;

/**
* @var GetShortObjectDescriptionAction
* @var RetrieveAutocompleteItemsAction
*/
private $action;

/**
* @var AdminInterface
* @var AdminInterface&MockObject
*/
private $admin;

Expand Down Expand Up @@ -230,12 +229,12 @@ public function testRetrieveAutocompleteItemsComplexProperty(): void
$this->assertSame('{"status":"OK","more":false,"items":[{"id":"123","label":"FOO"}]}', $response->getContent());
}

private function configureAutocompleteItemsDatagrid(): Stub
private function configureAutocompleteItemsDatagrid(): MockObject
{
$model = new \stdClass();

$targetAdmin = $this->createMock(AdminInterface::class);
$datagrid = $this->createStub(DatagridInterface::class);
$datagrid = $this->createMock(DatagridInterface::class);
$metadata = $this->createStub(MetadataInterface::class);
$pager = $this->createStub(PagerInterface::class);
$fieldDescription = $this->createStub(FieldDescriptionInterface::class);
Expand Down
Loading