Symfony controller action method should not be too long

  • Major
  • Readability

More information: https://insight.sensiolabs.com/what-we-analyse/symfony.controller.action_method_too_long

86% of all actions have more than 20 lines. This violation is raised when more than 10% actions are too long.

Time to fix: about 2 hours
Open Issue Permalink
Collective

Occurences of the rule violations:

Public methods in controller classes should only be actions (with the <code>Action</code> suffix) 10

  • Major
  • Architecture

More information: https://insight.sensiolabs.com/what-we-analyse/symfony.controller.non_action_public_method_in_controller_class

  1. class CoreController extends Controller
  2. {
  3. /**
  4. * @return string
  5. */
  6. public function getBaseTemplate()

    Non-action public method found in controller class

    Time to fix: about 1 hour
    Open Issue Permalink
    Collective
  7. {
  8. if ($this->getRequest()->isXmlHttpRequest()) {
  9. return $this->container->get('sonata.admin.pool')->getTemplate('ajax');
  10. }
  1. * @param integer $status
  2. * @param array $headers
  3. *
  4. * @return Response with json encoded data
  5. */
  6. public function renderJson($data, $status = 200, $headers = array())

    Non-action public method found in controller class

    Time to fix: about 1 hour
    Open Issue Permalink
    Collective
  7. {
  8. // fake content-type so browser does not show the download popup when this
  9. // response is rendered through an iframe (used by the jquery.form.js plugin)
  10. // => don't know yet if it is the best solution
  11. if ($this->get('request')->get('_xml_http_request')
  1. /**
  2. *
  3. * @return boolean true if the request is done by an ajax like query
  4. */
  5. public function isXmlHttpRequest()

    Non-action public method found in controller class

    Time to fix: about 1 hour
    Open Issue Permalink
    Collective
  6. {
  7. return $this->get('request')->isXmlHttpRequest() || $this->get('request')->get('_xml_http_request');
  8. }
  9. /**
  1. /**
  2. * Sets the Container associated with this Controller.
  3. *
  4. * @param ContainerInterface $container A ContainerInterface instance
  5. */
  6. public function setContainer(ContainerInterface $container = null)

    Non-action public method found in controller class

    Time to fix: about 1 hour
    Open Issue Permalink
    Collective
  7. {
  8. $this->container = $container;
  9. $this->configure();
  10. }
  1. * Contextualize the admin class depends on the current request
  2. *
  3. * @throws \RuntimeException
  4. * @return void
  5. */
  6. public function configure()

    Non-action public method found in controller class

    Time to fix: about 1 hour
    Open Issue Permalink
    Collective
  7. {
  8. $adminCode = $this->container->get('request')->get('_sonata_admin');
  9. if (!$adminCode) {
  10. throw new \RuntimeException(sprintf('There is no `_sonata_admin` defined for the controller `%s` and the current route `%s`', get_class($this), $this->container->get('request')->get('_route')));
  1. /**
  2. * return the base template name
  3. *
  4. * @return string the template name
  5. */
  6. public function getBaseTemplate()

    Non-action public method found in controller class

    Time to fix: about 1 hour
    Open Issue Permalink
    Collective
  7. {
  8. if ($this->isXmlHttpRequest()) {
  9. return $this->admin->getTemplate('ajax');
  10. }
  1. * @param array $parameters
  2. * @param Response $response
  3. *
  4. * @return Response
  5. */
  6. public function render($view, array $parameters = array(), Response $response = null)

    Non-action public method found in controller class

    Time to fix: about 1 hour
    Open Issue Permalink
    Collective
  7. {
  8. $parameters['admin'] = isset($parameters['admin']) ? $parameters['admin'] : $this->admin;
  9. $parameters['base_template'] = isset($parameters['base_template']) ? $parameters['base_template'] : $this->getBaseTemplate();
  10. $parameters['admin_pool'] = $this->get('sonata.admin.pool');
  1. *
  2. * @param \Sonata\AdminBundle\Datagrid\ProxyQueryInterface $query
  3. *
  4. * @return \Symfony\Component\HttpFoundation\RedirectResponse
  5. */
  6. public function batchActionDelete(ProxyQueryInterface $query)

    Non-action public method found in controller class

    Time to fix: about 1 hour
    Open Issue Permalink
    Collective
  7. {
  8. if (false === $this->admin->isGranted('DELETE')) {
  9. throw new AccessDeniedException();
  10. }
  1. *
  2. * @param object $object
  3. *
  4. * @return Response
  5. */
  6. public function redirectTo($object)

    Non-action public method found in controller class

    Time to fix: about 1 hour
    Open Issue Permalink
    Collective
  7. {
  8. $url = false;
  9. if ($this->get('request')->get('btn_update_and_list')) {
  10. $url = $this->admin->generateUrl('list');
  1. * Adds a flash message for type.
  2. *
  3. * @param string $type
  4. * @param string $message
  5. */
  6. public function addFlash($type, $message)

    Non-action public method found in controller class

    Time to fix: about 1 hour
    Open Issue Permalink
    Collective
  7. {
  8. $this->get('session')
  9. ->getFlashBag()
  10. ->add($type, $message);
  11. }

Files should not be executable

  • Major
  • Security

More information: https://insight.sensiolabs.com/what-we-analyse/php.too_permissive_file_permissions

Your project contains files with permissive permissions. In order to avoid opening a security breach, you should restrict execution rights on following files:

Time to fix: about 30 minutes
Open Issue Permalink
Collective

Object parameters should be type hinted 3

  • Minor
  • Bugrisk

More information: https://insight.sensiolabs.com/what-we-analyse/php.object_parameter_not_type_hinted

  1. * @param integer $status
  2. * @param array $headers
  3. *
  4. * @return Response with json encoded data
  5. */
  6. public function renderJson($data, $status = 200, $headers = array())

    The parameter headers, which is an object, should be typehinted.

    Time to fix: about 1 hour
    Open Issue Permalink
    Collective
  7. {
  8. // fake content-type so browser does not show the download popup when this
  9. // response is rendered through an iframe (used by the jquery.form.js plugin)
  10. // => don't know yet if it is the best solution
  11. if ($this->get('request')->get('_xml_http_request')
in Admin/AdminHelper.php, line 159
  1. * @param object $object
  2. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  3. *
  4. * @throws \RuntimeException
  5. */
  6. public function addNewInstance($object, FieldDescriptionInterface $fieldDescription)

    The parameter object, which is an object, should be typehinted.

    Time to fix: about 1 hour
    Open Issue Permalink
    Collective
  7. {
  8. $instance = $fieldDescription->getAssociationAdmin()->getNewInstance();
  9. $mapping = $fieldDescription->getAssociationMapping();
  10. $method = sprintf('add%s', $this->camelize($mapping['fieldName']));
  1. }
  2. /**
  3. * {@inheritdoc}
  4. */
  5. public function getFieldValue($object, $fieldName)

    The parameter object, which is an object, should be typehinted.

    Time to fix: about 1 hour
    Open Issue Permalink
    Collective
  6. {
  7. $camelizedFieldName = self::camelize($fieldName);
  8. $getters = array();
  9. // prefer method name given in the code option

User specific files should not appear in .gitignore 3

  • Minor
  • Codestyle

More information: https://insight.sensiolabs.com/what-we-analyse/git.user_specific_ignored_file

in .gitignore, line 1
  1. .idea

    .idea is user-specific and should not appear in a project .gitignore. Consider adding it to the user global .gitignore instead.

    Time to fix: about 15 minutes
    Open Issue Permalink
    Collective
  2. .DS_Store
  3. build
  4. phpunit.xml
  5. Resources/doc/_build/*
  6. nbproject
in .gitignore, line 2
  1. .idea
  2. .DS_Store

    .DS_Store is user-specific and should not appear in a project .gitignore. Consider adding it to the user global .gitignore instead.

    Time to fix: about 15 minutes
    Open Issue Permalink
    Collective
  3. build
  4. phpunit.xml
  5. Resources/doc/_build/*
  6. nbproject
  7. coverage
in .gitignore, line 6
  1. .idea
  2. .DS_Store
  3. build
  4. phpunit.xml
  5. Resources/doc/_build/*
  6. nbproject

    nbproject is user-specific and should not appear in a project .gitignore. Consider adding it to the user global .gitignore instead.

    Time to fix: about 15 minutes
    Open Issue Permalink
    Collective
  7. coverage
  8. composer.lock
  9. vendor
  10. composer.phar

Unused method, property, variable or parameter 2

  • Minor
  • Deadcode

More information: https://insight.sensiolabs.com/what-we-analyse/php.unused_local_variable_or_private_member

in Admin/Admin.php, line 104
  1. protected $formFieldDescriptions = array();
  2. /**
  3. * @var \Sonata\AdminBundle\Datagrid\DatagridInterface
  4. */
  5. private $filter;

    This filter attribute is declared but never used. You should remove it.

    Time to fix: about 15 minutes
    Open Issue Permalink
    Collective
  6. /**
  7. * The filter FieldDescription constructed from the configureFilterField method
  8. *
  9. * @var array
  1. *
  2. * @param string $property The name of the property
  3. *
  4. * @return \ReflectionProperty The reflection instance
  5. */
  6. private function getReflProperty($property)

    This getReflProperty method is declared but never used. You should remove it.

    Time to fix: about 15 minutes
    Open Issue Permalink
    Collective
  7. {
  8. if (!isset($this->reflProperties[$property])) {
  9. $this->reflProperties[$property] = new \ReflectionProperty($this->class, $property);
  10. $this->reflProperties[$property]->setAccessible(true);
  11. }

Unused use statement should be avoided 5

  • Minor
  • Deadcode

More information: https://insight.sensiolabs.com/what-we-analyse/php.unused_use_statement

  1. namespace Sonata\AdminBundle\Form\ChoiceList;
  2. use Symfony\Component\PropertyAccess\PropertyAccess;
  3. use Symfony\Component\PropertyAccess\PropertyPath;
  4. use Symfony\Component\Form\Exception\FormException;

    The class Symfony\Component\Form\Exception\FormException is declared but never used. You should remove the use statement.

    Time to fix: about 15 minutes
    Open Issue Permalink
    Collective
  5. use Symfony\Component\Form\Extension\Core\ChoiceList\SimpleChoiceList;
  6. use Sonata\AdminBundle\Model\ModelManagerInterface;
  7. class ModelChoiceList extends SimpleChoiceList
  8. {
  1. */
  2. namespace Sonata\AdminBundle\Builder;
  3. use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
  4. use Sonata\AdminBundle\Admin\AdminInterface;

    The class Sonata\AdminBundle\Admin\AdminInterface is declared but never used. You should remove the use statement.

    Time to fix: about 15 minutes
    Open Issue Permalink
    Collective
  5. use Symfony\Component\Form\FormBuilder;
  6. use Symfony\Component\Form\FormFactoryInterface;
  7. interface FormContractorInterface extends BuilderInterface
  1. * file that was distributed with this source code.
  2. */
  3. namespace Sonata\AdminBundle\Controller;
  4. use Symfony\Bundle\FrameworkBundle\Controller\Controller;

    The class Symfony\Bundle\FrameworkBundle\Controller\Controller is declared but never used. You should remove the use statement.

    Time to fix: about 15 minutes
    Open Issue Permalink
    Collective
  5. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\PropertyAccess\PropertyAccess;
  9. use Symfony\Component\PropertyAccess\PropertyPath;
  1. * file that was distributed with this source code.
  2. */
  3. namespace Sonata\AdminBundle\DependencyInjection\Compiler;
  4. use Symfony\Component\DependencyInjection\Definition;

    The class Symfony\Component\DependencyInjection\Definition is declared but never used. You should remove the use statement.

    Time to fix: about 15 minutes
    Open Issue Permalink
    Collective
  5. use Symfony\Component\DependencyInjection\ContainerBuilder;
  6. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  7. use Symfony\Component\DependencyInjection\ContainerInterface;
  8. /*
  1. namespace Sonata\AdminBundle\DependencyInjection;
  2. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  3. use Symfony\Component\DependencyInjection\ContainerBuilder;
  4. use Symfony\Component\DependencyInjection\Definition;

    The class Symfony\Component\DependencyInjection\Definition is declared but never used. You should remove the use statement.

    Time to fix: about 15 minutes
    Open Issue Permalink
    Collective
  5. use Symfony\Component\HttpKernel\DependencyInjection\Extension;
  6. use Symfony\Component\Config\FileLocator;
  7. use Symfony\Component\Config\Definition\Processor;

PHP classes should not use magic methods

  • Info
  • Codestyle

More information: https://insight.sensiolabs.com/what-we-analyse/php.php_magic_methods

  1. * @param string $name
  2. * @param array $arguments
  3. *
  4. * @return ErrorElement
  5. */
  6. public function __call($name, array $arguments = array())

    __call() is a magic method, and may hide the class API to the user. Unless dynamic methods name is a string requirement, consider moving this method logic to a non-magical method.

    Time to fix: about 1 day
    Open Issue Permalink
    Collective
  7. {
  8. if (substr($name, 0, 6) == 'assert') {
  9. $this->validate($this->newConstraint(substr($name, 6), isset($arguments[0]) ? $arguments[0] : array()));
  10. } else {
  11. throw new \RunTimeException('Unable to recognize the command');