Completed
Push — master ( 1db3cd...632e40 )
by Jeroen
24:52 queued 11:31
created

Kunstmaan/AdminListBundle/AdminList/ExportList.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\AdminListBundle\AdminList;
4
5
use Kunstmaan\AdminListBundle\AdminList\Configurator\ExportListConfiguratorInterface;
6
use Symfony\Component\HttpFoundation\Request;
7
8
class ExportList implements ExportableInterface
9
{
10
    /**
11
     * @var ExportListConfiguratorInterface
12
     */
13
    private $configurator;
14
15
    public function __construct(ExportListConfiguratorInterface $configurator)
16
    {
17
        $this->configurator = $configurator;
18
        $this->configurator->buildFilters();
19
        $this->configurator->buildExportFields();
20
        $this->configurator->buildIterator();
21
    }
22
23
    /**
24
     * @param Request $request
25
     *
26
     * @deprecated ? This method is not configured in the configurator interface
27
     */
28
    public function bindRequest(Request $request)
29
    {
30
        $this->configurator->bindRequest($request);
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Kunstmaan\AdminListBundl...stConfiguratorInterface as the method bindRequest() does only exist in the following implementations of said interface: Kunstmaan\AdminBundle\Ad...onAdminListConfigurator, Kunstmaan\AdminListBundl...ctAdminListConfigurator, Kunstmaan\AdminListBundl...ALAdminListConfigurator, Kunstmaan\AdminListBundl...RMAdminListConfigurator, Kunstmaan\AdminListBundl...geAdminListConfigurator, Kunstmaan\ArticleBundle\...orAdminListConfigurator, Kunstmaan\ArticleBundle\...ryAdminListConfigurator, Kunstmaan\ArticleBundle\...geAdminListConfigurator, Kunstmaan\ArticleBundle\...agAdminListConfigurator, Kunstmaan\ArticleBundle\...\AdminList\Configurator, Kunstmaan\FormBundle\Adm...geAdminListConfigurator, Kunstmaan\FormBundle\Adm...onAdminListConfigurator, Kunstmaan\LeadGeneration...upAdminListConfigurator, Kunstmaan\LeadGeneration...esAdminListConfigurator, Kunstmaan\MediaBundle\Ad...iaAdminListConfigurator, Kunstmaan\MenuBundle\Adm...nuAdminListConfigurator, Kunstmaan\MenuBundle\Adm...emAdminListConfigurator, Kunstmaan\NodeBundle\Adm...deAdminListConfigurator, Kunstmaan\RedirectBundle...ctAdminListConfigurator, Kunstmaan\TaggingBundle\...agAdminListConfigurator, Kunstmaan\TranslatorBund...onAdminListConfigurator, Kunstmaan\UserManagement...gsAdminListConfigurator, Kunstmaan\UserManagement...upAdminListConfigurator, Kunstmaan\UserManagement...leAdminListConfigurator, Kunstmaan\UserManagement...erAdminListConfigurator.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
31
    }
32
33
    public function getExportColumns()
34
    {
35
        return $this->configurator->getExportFields();
36
    }
37
38
    public function getIterator()
39
    {
40
        return $this->configurator->getIterator();
41
    }
42
43
    public function getStringValue($item, $columnName)
44
    {
45
        return $this->configurator->getStringValue($item, $columnName);
46
    }
47
}
48