Skip to content

Commit

Permalink
bump PHPStan to level 4
Browse files Browse the repository at this point in the history
  • Loading branch information
vv12131415 authored and jordisala1991 committed Aug 10, 2020
1 parent cd4ff08 commit 3fa3f49
Show file tree
Hide file tree
Showing 16 changed files with 168 additions and 44 deletions.
93 changes: 89 additions & 4 deletions .phpstan/phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,94 @@ parameters:

-
# will be fixed in v4. Code is marked as deprecated
message: "#^Method Sonata\\\\AdminBundle\\\\Admin\\\\AbstractAdmin\\:\\:getFormFieldDescription\\(\\) should return Sonata\\\\AdminBundle\\\\Admin\\\\FieldDescriptionInterface but returns null\\.$#"
message: "#^Method Sonata\\\\AdminBundle\\\\Admin\\\\AbstractAdmin\\:\\:getShowFieldDescription\\(\\) should return Sonata\\\\AdminBundle\\\\Admin\\\\FieldDescriptionInterface but returns null\\.$#"
count: 1
path: ../src/Admin/AbstractAdmin.php

-
# will be fixed in v4. Code is marked as deprecated
message: "#^Method Sonata\\\\AdminBundle\\\\Admin\\\\AbstractAdmin\\:\\:getShowFieldDescription\\(\\) should return Sonata\\\\AdminBundle\\\\Admin\\\\FieldDescriptionInterface but returns null\\.$#"
message: "#^Result of \\&\\& is always false\\.$#"
count: 1
path: ../src/Admin/AbstractAdmin.php

-
# will be fixed in v4. Code is marked as deprecated
message: "#^Method Sonata\\\\AdminBundle\\\\Admin\\\\AbstractAdmin\\:\\:getListFieldDescription\\(\\) should return Sonata\\\\AdminBundle\\\\Admin\\\\FieldDescriptionInterface but returns null\\.$#"
message: "#^Strict comparison using \\!\\=\\= between 'Sonata\\\\\\\\AdminBundle\\\\\\\\Admin\\\\\\\\AdminHelper' and 'Sonata\\\\\\\\AdminBundle\\\\\\\\Admin\\\\\\\\AdminHelper' will always evaluate to false\\.$#"
count: 1
path: ../src/Admin/AbstractAdmin.php
path: ../src/Admin/AdminHelper.php

-
# will be fixed in v4. Code is marked as deprecated
message: "#^Result of && is always false\\.$#"
count: 2
path: ../src/Admin/BaseFieldDescription.php

-
# will be fixed in v4. Code is marked as deprecated
message: "#^Else branch is unreachable because ternary operator condition is always true\\.$#"
count: 1
path: ../src/Block/AdminListBlockService.php

-
# will be fixed in v4. Code is marked as deprecated
message: "#^Call to function is_object\\(\\) with string will always evaluate to false\\.$#"
count: 1
path: ../src/Block/AdminListBlockService.php

-
# will be fixed in v4. Code is marked as deprecated
message: "#^Result of \\|\\| is always true\\.$#"
count: 1
path: ../src/Block/AdminListBlockService.php

-
# will be fixed in v4. Code is marked as deprecated
message: "#^Call to function is_object\\(\\) with string will always evaluate to false\\.$#"
count: 1
path: ../src/Block/AdminSearchBlockService.php

-
# will be fixed in v4. Code is marked as deprecated
message: "#^Result of \\|\\| is always true\\.$#"
count: 1
path: ../src/Block/AdminSearchBlockService.php

-
# will be fixed in v4. Code is marked as deprecated
message: "#^Call to function is_object\\(\\) with string will always evaluate to false\\.$#"
count: 1
path: ../src/Block/AdminStatsBlockService.php

-
# will be fixed in v4. Code is marked as deprecated
message: "#^Result of \\|\\| is always true\\.$#"
count: 1
path: ../src/Block/AdminStatsBlockService.php


-
# will be fixed in v4. Code is marked as deprecated
message: "#^Result of && is always false\\.$#"
count: 2
path: ../src/Command/GenerateObjectAclCommand.php

-
# will be fixed in v4. Code is marked as deprecated
message: "#^Instanceof between \\*NEVER\\* and Doctrine\\\\Persistence\\\\ManagerRegistry will always evaluate to false\\.$#"
count: 1
path: ../src/Command/GenerateObjectAclCommand.php

-
# will be fixed in v4. Code is marked as deprecated
message: "#^Else branch is unreachable because ternary operator condition is always true\\.$#"
count: 1
path: ../src/Command/GenerateObjectAclCommand.php

-
# will be fixed in v4. Code is marked as deprecated
message: "#^Strict comparison using \\!\\=\\= between 'Sonata\\\\\\\\AdminBundle\\\\\\\\Command\\\\\\\\GenerateObjectAclCommand' and 'Sonata\\\\\\\\AdminBundle\\\\\\\\Command\\\\\\\\GenerateObjectAclCommand' will always evaluate to false\\.$#"
count: 1
path: ../src/Command/GenerateObjectAclCommand.php

-
# will be fixed in v4. Currently BC break
Expand Down Expand Up @@ -108,6 +181,18 @@ parameters:
count: 1
path: ../src/Form/FormMapper.php

-
# will be fixed in v4. Code is marked as deprecated
message: "#^Right side of && is always true\\.$#"
count: 1
path: ../src/Mapper/BaseGroupedMapper.php

-
# will be fixed in v4. Currently BC break
message: "#^Negated boolean expression is always false\\.$#"
count: 1
path: ../src/Route/RoutesCache.php

-
# will be fixed in v4. Currently BC break
message: "#^Method Sonata\\\\AdminBundle\\\\Admin\\\\FieldDescriptionInterface\\:\\:getLabel\\(\\) invoked with 1 parameter, 0 required\\.$#"
Expand Down
3 changes: 1 addition & 2 deletions .phpstan/phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ includes:
- phpstan-baseline.neon

parameters:
level: 3

level: 4
paths:
- ../src
excludes_analyse:
Expand Down
10 changes: 6 additions & 4 deletions src/Action/GetShortObjectDescriptionAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public function __invoke(Request $request): Response
$uniqid = $request->get('uniqid');
$linkParameters = $request->get('linkParameters', []);

$admin = $this->pool->getInstance($code);

if (!$admin) {
try {
$admin = $this->pool->getInstance($code);
} catch (\InvalidArgumentException $e) {
throw new NotFoundHttpException(sprintf('Could not find admin for code "%s"', $code));
}

Expand All @@ -75,7 +75,9 @@ public function __invoke(Request $request): Response
'id' => $admin->id($object),
'label' => $admin->toString($object),
]]);
} elseif ('html' === $request->get('_format')) {
}

if ('html' === $request->get('_format')) {
return new Response($this->twig->render($admin->getTemplate('short_object_description'), [
'admin' => $admin,
'description' => $admin->toString($object),
Expand Down
11 changes: 9 additions & 2 deletions src/Admin/AbstractAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3201,7 +3201,14 @@ final public function isDefaultFilter($name)
*/
public function canAccessObject($action, $object)
{
return $object && $this->id($object) && $this->hasAccess($action, $object);
if (!\is_object($object)) {
return false;
}
if (!$this->id($object)) {
return false;
}

return $this->hasAccess($action, $object);
}

protected function configureQuery(ProxyQueryInterface $query): ProxyQueryInterface
Expand Down Expand Up @@ -3488,7 +3495,7 @@ protected function predefinePerPageOptions()
/**
* Return list routes with permissions name.
*
* @return array<string, string>
* @return array<string, string|array>
*/
protected function getAccess()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Admin/AdminInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ public function getSubject();
*
* @param string $name
*
* @return FieldDescriptionInterface
* @return FieldDescriptionInterface|null // NEXT_MAJOR: Return FieldDescriptionInterface
*/
public function getListFieldDescription($name);

Expand Down
2 changes: 1 addition & 1 deletion src/Admin/FieldDescriptionRegistryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ interface FieldDescriptionRegistryInterface
*
* @param string $name
*
* @return FieldDescriptionInterface
* @return FieldDescriptionInterface|null // NEXT_MAJOR: Return FieldDescriptionInterface
*/
public function getFormFieldDescription($name);

Expand Down
2 changes: 1 addition & 1 deletion src/Controller/CRUDController.php
Original file line number Diff line number Diff line change
Expand Up @@ -1384,7 +1384,7 @@ protected function getAclRoles()

$aclRoles = array_unique($aclRoles);

return \is_array($aclRoles) ? new \ArrayIterator($aclRoles) : $aclRoles;
return new \ArrayIterator($aclRoles);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Datagrid/Pager.php
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ public function setQuery($query)
}

/**
* @return ProxyQueryInterface
* @return ProxyQueryInterface|null
*/
public function getQuery()
{
Expand Down
4 changes: 2 additions & 2 deletions src/Form/ChoiceList/ModelChoiceLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class ModelChoiceLoader implements ChoiceLoaderInterface
private $choices;

/**
* @var PropertyPath
* @var PropertyPath|null
*/
private $propertyPath;

Expand Down Expand Up @@ -102,7 +102,7 @@ public function loadChoiceList($value = null)
}

$choices = [];
foreach ($entities as $key => $model) {
foreach ($entities as $model) {
if ($this->propertyPath) {
// If the property option was given, use it
$valueObject = $this->propertyAccessor->getValue($model, $this->propertyPath);
Expand Down
4 changes: 2 additions & 2 deletions src/Form/Extension/Field/Type/FormTypeFieldExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public function configureOptions(OptionsResolver $resolver)
* return the value related to FieldDescription, if the associated object does no
* exists => a temporary one is created.
*
* @param object $object
* @param object|null $object
*
* @return mixed
*/
Expand All @@ -209,7 +209,7 @@ public function getValueFromFieldDescription($object, FieldDescriptionInterface
$value = null;

if (!$object) {
return $value;
return null;
}

try {
Expand Down
32 changes: 14 additions & 18 deletions src/Maker/AdminMaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
'Controller'
);

$this->generateController($input, $io, $generator, $controllerClassNameDetails);
$this->generateController($io, $generator, $controllerClassNameDetails);

$controllerClassFullName = $controllerClassNameDetails->getFullName();
}
Expand Down Expand Up @@ -225,27 +225,23 @@ private function generateService(
}

private function generateController(
InputInterface $input,
ConsoleStyle $io,
Generator $generator,
ClassNameDetails $controllerClassNameDetails
): void {
$controllerClassFullName = null;
if ($controllerClassNameDetails) {
$controllerClassFullName = $controllerClassNameDetails->getFullName();
$generator->generateClass(
$controllerClassFullName,
sprintf('%s/AdminController.tpl.php', $this->skeletonDirectory),
[]
);
$generator->writeChanges();
$io->writeln(sprintf(
'%sThe controller class "<info>%s</info>" has been generated under the file "<info>%s</info>".',
PHP_EOL,
$controllerClassNameDetails->getShortName(),
$controllerClassFullName
));
}
$controllerClassFullName = $controllerClassNameDetails->getFullName();
$generator->generateClass(
$controllerClassFullName,
sprintf('%s/AdminController.tpl.php', $this->skeletonDirectory),
[]
);
$generator->writeChanges();
$io->writeln(sprintf(
'%sThe controller class "<info>%s</info>" has been generated under the file "<info>%s</info>".',
PHP_EOL,
$controllerClassNameDetails->getShortName(),
$controllerClassFullName
));
}

private function generateAdmin(
Expand Down
5 changes: 4 additions & 1 deletion src/Menu/Provider/GroupMenuProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,14 @@ public function __construct(FactoryInterface $menuFactory, Pool $pool, $checker
*/
public function get($name, array $options = [])
{
/**
* @var array{ label: string, label_catalogue: string, icon: string, on_top?: bool, keep_open: bool, provider: string, items: list }
*/
$group = $options['group'];

$menuItem = $this->menuFactory->createItem($options['name']);

if (empty($group['on_top']) || false === $group['on_top']) {
if (!\array_key_exists('on_top', $group) || false === $group['on_top']) {
foreach ($group['items'] as $item) {
if ($this->canGenerateMenuItem($item, $group)) {
$menuItem->addChild($this->generateMenuItem($item, $group));
Expand Down
2 changes: 1 addition & 1 deletion src/Twig/Extension/SonataAdminExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ private function render(
TemplateWrapper $template,
array $parameters,
Environment $environment
): ?string {
): string {
$content = $template->render($parameters);

if ($environment->isDebug()) {
Expand Down
5 changes: 3 additions & 2 deletions tests/Action/GetShortObjectDescriptionActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,15 @@ protected function setUp(): void
public function testGetShortObjectDescriptionActionInvalidAdmin(): void
{
$this->expectException(NotFoundHttpException::class);
$code = 'sonata.post.admin';

$request = new Request([
'code' => 'sonata.post.admin',
'code' => $code,
'objectId' => 42,
'uniqid' => 'asdasd123',
]);

$this->pool->getInstance('sonata.post.admin')->willReturn(null);
$this->pool->getInstance($code)->willThrow(\InvalidArgumentException::class);
$this->admin->setRequest(Argument::type(Request::class))->shouldNotBeCalled();

($this->action)($request);
Expand Down
30 changes: 30 additions & 0 deletions tests/Admin/AdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2129,6 +2129,36 @@ public function testGetActionButtonsListCreateDisabled(): void
$this->assertSame([], $admin->getActionButtons('list', null));
}

public function testCantAccessObjectIfNullPassed(): void
{
$admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');

$this->assertFalse($admin->canAccessObject('list', null));
}

public function testCantAccessObjectIfRandomObjectPassed(): void
{
$admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
$modelManager = $this->createMock(ModelManagerInterface::class);
$admin->setModelManager($modelManager);

$this->assertFalse($admin->canAccessObject('list', new \stdClass()));
}

public function testCanAccessObject(): void
{
$admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
$modelManager = $this->createMock(ModelManagerInterface::class);
$modelManager
->method('getNormalizedIdentifier')
->willReturn('identifier');
$admin->setModelManager($modelManager);
$securityHandler = $this->createMock(SecurityHandlerInterface::class);
$admin->setSecurityHandler($securityHandler);

$this->assertTrue($admin->canAccessObject('list', new \stdClass()));
}

/**
* @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configureBatchActions
*/
Expand Down
5 changes: 3 additions & 2 deletions tests/Controller/HelperControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,15 @@ protected function setUp(): void
public function testGetShortObjectDescriptionActionInvalidAdmin(): void
{
$this->expectException(NotFoundHttpException::class);
$code = 'sonata.post.admin';

$request = new Request([
'code' => 'sonata.post.admin',
'code' => $code,
'objectId' => 42,
'uniqid' => 'asdasd123',
]);

$this->pool->getInstance('sonata.post.admin')->willReturn(null);
$this->pool->getInstance($code)->willThrow(\InvalidArgumentException::class);
$this->admin->setRequest(Argument::type(Request::class))->shouldNotBeCalled();

$this->controller->getShortObjectDescriptionAction($request);
Expand Down

0 comments on commit 3fa3f49

Please sign in to comment.