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

Add PHPStan check #336

Closed
wants to merge 3 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
12 changes: 12 additions & 0 deletions .github/workflows/qa.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
on: [push, pull_request]
name: Quality assurance
jobs:
phpstan:
name: PHPStan
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: PHPStan
uses: "docker://oskarstark/phpstan-ga"
Copy link
Member

Choose a reason for hiding this comment

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

@0.12.2 ?

Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason, why we shouldn't use the latest version? We're doing the same for every other QA tool (php-cs-fixer, phpunit, ...)

Copy link
Member

Choose a reason for hiding this comment

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

Ok in this case

Copy link
Member Author

Choose a reason for hiding this comment

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

What about the PHP extension mongodb? can I do something?

with:
args: analyse
30 changes: 30 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
parameters:
level: 1

paths:
- src
# Uncomment this, when phpstan extensions are allowed
# - tests

excludes_analyse:
- src/Test/AbstractWidgetTestCase.php
- tests/bootstrap.php

autoload_files:
- vendor/autoload.php

ignoreErrors:
-
message: "#^Instantiated class MongoId not found\\.$#"
count: 1
path: src/Filter/ModelFilter.php

-
message: "#^Caught class MongoException not found\\.$#"
count: 1
path: src/Filter/ModelFilter.php

-
message: "#^Instantiated class MongoRegex not found\\.$#"
count: 1
path: src/Filter/StringFilter.php
2 changes: 1 addition & 1 deletion src/Admin/FieldDescription.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function setParentAssociationMappings(array $parentAssociationMappings)
*/
public function isIdentifier()
{
return isset($this->fieldMapping['id']) ? $this->fieldMapping['id'] : false;
return $this->fieldMapping['id'] ?? false;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Builder/DatagridBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInter
$fieldDescription->setAdmin($admin);

if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
list($metadata, $lastPropertyName, $parentAssociationMappings) = $admin->getModelManager()->getParentMetadataForProperty($admin->getClass(), $fieldDescription->getName());
[$metadata, $lastPropertyName, $parentAssociationMappings] = $admin->getModelManager()->getParentMetadataForProperty($admin->getClass(), $fieldDescription->getName());

// set the default field mapping
if (isset($metadata->fieldMappings[$lastPropertyName])) {
Expand Down
2 changes: 1 addition & 1 deletion src/Builder/ListBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInter
$fieldDescription->setAdmin($admin);

if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
list($metadata, $lastPropertyName, $parentAssociationMappings) = $admin->getModelManager()->getParentMetadataForProperty($admin->getClass(), $fieldDescription->getName());
[$metadata, $lastPropertyName, $parentAssociationMappings] = $admin->getModelManager()->getParentMetadataForProperty($admin->getClass(), $fieldDescription->getName());
$fieldDescription->setParentAssociationMappings($parentAssociationMappings);

// set the default field mapping
Expand Down
2 changes: 1 addition & 1 deletion src/Builder/ShowBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInter
$fieldDescription->setAdmin($admin);

if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
list($metadata, $lastPropertyName, $parentAssociationMappings) = $admin->getModelManager()->getParentMetadataForProperty($admin->getClass(), $fieldDescription->getName());
[$metadata, $lastPropertyName, $parentAssociationMappings] = $admin->getModelManager()->getParentMetadataForProperty($admin->getClass(), $fieldDescription->getName());
$fieldDescription->setParentAssociationMappings($parentAssociationMappings);

// set the default field mapping
Expand Down
8 changes: 0 additions & 8 deletions src/Datagrid/Pager.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,6 @@ public function getResults()
return $this->getQuery()->execute();
}

/**
* {@inheritdoc}
*/
public function getQuery()
{
return $this->query;
}

/**
* {@inheritdoc}
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Datagrid/ProxyQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function execute(array $params = [], $hydrationMode = null)
$queryBuilder->sort($sortBy, $this->getSortOrder());
}

return $queryBuilder->getQuery()->execute($params, $hydrationMode);
return $queryBuilder->getQuery()->execute();
}

public function setSortBy($parentAssociationMappings, $fieldMapping)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ public function load(array $configs, ContainerBuilder $container)
$processor = new Processor();
$config = $processor->processConfiguration($configuration, $configs);

$pool = $container->getDefinition('sonata.admin.manager.doctrine_mongodb');

$container->setParameter('sonata_doctrine_mongodb_admin.templates', $config['templates']);

// define the templates
Expand Down
36 changes: 35 additions & 1 deletion src/Filter/AbstractDateFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,34 +49,44 @@ public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
$data['type'] = !isset($data['type']) || !is_numeric($data['type']) ? DateType::TYPE_EQUAL : $data['type'];

// Some types do not require a value to be set (NULL, NOT NULL).
if (!$this->typeRequiresValue($data['type']) && !$data['value']) {
if (!($data['value'] ?? false) && !$this->typeRequiresValue($data['type'])) {
return;
}

switch ($data['type']) {
case DateType::TYPE_EQUAL:
$this->active = true;

return $this->applyTypeIsEqual($queryBuilder, $field, $data);

case DateType::TYPE_GREATER_THAN:
if (!\array_key_exists('value', $data) || !$data['value']) {
return;
}

$this->active = true;

return $this->applyTypeIsGreaterThan($queryBuilder, $field, $data);

case DateType::TYPE_LESS_EQUAL:
if (!\array_key_exists('value', $data) || !$data['value']) {
return;
}

$this->active = true;

return $this->applyTypeIsLessEqual($queryBuilder, $field, $data);

case DateType::TYPE_NULL:
case DateType::TYPE_NOT_NULL:
$this->active = true;

return $this->applyType($queryBuilder, $this->getOperator($data['type']), $field, null);

case DateType::TYPE_GREATER_EQUAL:
case DateType::TYPE_LESS_THAN:
$this->active = true;

return $this->applyType($queryBuilder, $this->getOperator($data['type']), $field, $data['value']);
}
}
Expand Down Expand Up @@ -111,6 +121,30 @@ public function getRenderSettings()
]];
}

/**
* @param string $field
* @param array $data
*/
protected function applyTypeIsLessEqual(ProxyQueryInterface $queryBuilder, $field, $data)
{
}

/**
* @param string $field
* @param array $data
*/
protected function applyTypeIsGreaterThan(ProxyQueryInterface $queryBuilder, $field, $data)
{
}

/**
* @param string $field
* @param array $data
*/
protected function applyTypeIsEqual(ProxyQueryInterface $queryBuilder, $field, $data)
{
}

/**
* @param string $operation
* @param string $field
Expand Down
4 changes: 2 additions & 2 deletions src/Filter/BooleanFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
continue;
}

$values[] = (BooleanType::TYPE_YES === $v) ? true : false;
$values[] = BooleanType::TYPE_YES === $v;
}

if (0 === \count($values)) {
Expand All @@ -52,7 +52,7 @@ public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
return;
}

$value = BooleanType::TYPE_YES === $data['value'] ? true : false;
$value = BooleanType::TYPE_YES === $data['value'];

$queryBuilder->field($field)->equals($value);
$this->active = true;
Expand Down
6 changes: 6 additions & 0 deletions src/Filter/DateFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@
namespace Sonata\DoctrineMongoDBAdminBundle\Filter;

use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Symfony\Component\Form\Extension\Core\Type\DateType;

class DateFilter extends AbstractDateFilter
{
public function getFieldType()
{
return $this->getOption('field_type', DateType::class);
}

/**
* @param string $field
* @param array $data
Expand Down
6 changes: 6 additions & 0 deletions src/Filter/DateTimeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Sonata\DoctrineMongoDBAdminBundle\Filter;

use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;

class DateTimeFilter extends AbstractDateFilter
{
Expand All @@ -24,6 +25,11 @@ class DateTimeFilter extends AbstractDateFilter
*/
protected $time = true;

public function getFieldType()
{
return $this->getOption('field_type', DateTimeType::class);
}

/**
* @param string $field
* @param array $data
Expand Down
2 changes: 1 addition & 1 deletion src/Guesser/FilterTypeGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function guessType($class, $property, ModelManagerInterface $modelManager
'options' => [],
];

list($metadata, $propertyName, $parentAssociationMappings) = $ret;
[$metadata, $propertyName, $parentAssociationMappings] = $ret;

$options['parent_association_mappings'] = $parentAssociationMappings;

Expand Down
2 changes: 1 addition & 1 deletion src/Guesser/TypeGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function guessType($class, $property, ModelManagerInterface $modelManager
return new TypeGuess('text', [], Guess::LOW_CONFIDENCE);
}

list($metadata, $propertyName, $parentAssociationMappings) = $ret;
[$metadata, $propertyName,] = $ret;

if ($metadata->hasAssociation($propertyName)) {
$mapping = $metadata->fieldMappings[$propertyName];
Expand Down
23 changes: 17 additions & 6 deletions src/Model/ModelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Sonata\DoctrineMongoDBAdminBundle\Model;

use Doctrine\ODM\MongoDB\Query\Builder;
use Doctrine\ODM\MongoDB\Query\Query;
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
use Sonata\AdminBundle\Datagrid\DatagridInterface;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
Expand All @@ -22,7 +23,6 @@
use Sonata\DoctrineMongoDBAdminBundle\Datagrid\ProxyQuery;
use Sonata\Exporter\Source\DoctrineODMQuerySourceIterator;
use Symfony\Bridge\Doctrine\ManagerRegistry;
use Symfony\Component\Form\Exception\PropertyAccessDeniedException;

class ModelManager implements ModelManagerInterface
{
Expand Down Expand Up @@ -97,7 +97,7 @@ public function getNewFieldDescriptionInstance($class, $name, array $options = [
$options['route']['parameters'] = [];
}

list($metadata, $propertyName, $parentAssociationMappings) = $this->getParentMetadataForProperty($class, $name);
[$metadata, $propertyName, $parentAssociationMappings] = $this->getParentMetadataForProperty($class, $name);

$fieldDescription = new FieldDescription();
$fieldDescription->setName($name);
Expand Down Expand Up @@ -359,6 +359,17 @@ public function getExportFields($class)
*/
public function getModelInstance($class)
{
$r = new \ReflectionClass($class);
if ($r->isAbstract()) {
throw new \InvalidArgumentException(sprintf('Cannot initialize abstract class: %s', $class));
}

$constructor = $r->getConstructor();

if (null !== $constructor && (!$constructor->isPublic() || $constructor->getNumberOfRequiredParameters() > 0)) {
return $r->newInstanceWithoutConstructor();
}

return new $class();
}

Expand Down Expand Up @@ -443,7 +454,7 @@ public function modelReverseTransform($class, array $array = [])

if ($reflClass->hasMethod($setter)) {
if (!$reflClass->getMethod($setter)->isPublic()) {
throw new PropertyAccessDeniedException(sprintf('Method "%s()" is not public in class "%s"', $setter, $reflClass->getName()));
throw new \BadMethodCallException(sprintf('Method "%s()" is not public in class "%s"', $setter, $reflClass->getName()));
}

$instance->$setter($value);
Expand All @@ -452,7 +463,7 @@ public function modelReverseTransform($class, array $array = [])
$instance->$property = $value;
} elseif ($reflClass->hasProperty($property)) {
if (!$reflClass->getProperty($property)->isPublic()) {
throw new PropertyAccessDeniedException(sprintf('Property "%s" is not public in class "%s". Maybe you should create the method "set%s()"?', $property, $reflClass->getName(), ucfirst($property)));
throw new \BadMethodCallException(sprintf('Property "%s" is not public in class "%s". Maybe you should create the method "set%s()"?', $property, $reflClass->getName(), ucfirst($property)));
}

$instance->$property = $value;
Expand Down Expand Up @@ -505,14 +516,14 @@ public function collectionRemoveElement(&$collection, &$element)
}

/**
* method taken from PropertyPath.
* method taken from Symfony\Component\PropertyAccess\PropertyAccessor.
*
* @param string $property
*
* @return mixed
*/
protected function camelize($property)
{
return preg_replace(['/(^|_)+(.)/e', '/\.(.)/e'], ["strtoupper('\\2')", "'_'.strtoupper('\\1')"], $property);
return str_replace(' ', '', ucwords(str_replace('_', ' ', $property)));
}
}
5 changes: 3 additions & 2 deletions src/Util/ObjectAclManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public function batchConfigureAcls(OutputInterface $output, AdminInterface $admi
$batchSize = 20;
$batchSizeOutput = 200;
$objectIds = [];
$objectIdIterator = new \ArrayIterator();

foreach ($qb->getQuery()->iterate() as $row) {
$objectIds[] = ObjectIdentity::fromDomainObject($row);
Expand All @@ -62,7 +63,7 @@ public function batchConfigureAcls(OutputInterface $output, AdminInterface $admi
++$count;

if (0 === ($count % $batchSize)) {
list($batchAdded, $batchUpdated) = $this->configureAcls($output, $admin, $objectIdIterator, $securityIdentity);
[$batchAdded, $batchUpdated] = $this->configureAcls($output, $admin, $objectIdIterator, $securityIdentity);
$countAdded += $batchAdded;
$countUpdated += $batchUpdated;
$objectIds = [];
Expand All @@ -80,7 +81,7 @@ public function batchConfigureAcls(OutputInterface $output, AdminInterface $admi
}

if (\count($objectIds) > 0) {
list($batchAdded, $batchUpdated) = $this->configureAcls($output, $admin, $objectIdIterator, $securityIdentity);
[$batchAdded, $batchUpdated] = $this->configureAcls($output, $admin, $objectIdIterator, $securityIdentity);
$countAdded += $batchAdded;
$countUpdated += $batchUpdated;
}
Expand Down
Loading