Completed
Push — master ( b77a89...ae9c75 )
by Vitaly
06:10
created

Status::render()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 25
Code Lines 21

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 25
rs 8.439
cc 5
eloc 21
nc 5
nop 3
1
<?php
2
3
namespace samsoncms\newsletter\field;
4
5
use samsoncms\field\Generic;
6
use samsonframework\core\RenderInterface;
7
use samsonframework\orm\QueryInterface;
8
9
/**
10
 * Overridden control field
11
 * @package samsoncms\app\user
12
 */
13
class Status extends Generic
14
{
15
    /** @var string Path to field view file */
16
    protected $innerView = 'www/field/status';
17
18
    /**
19
     * Render collection entity field inner block
20
     * @param RenderInterface $renderer
21
     * @param QueryInterface $query
22
     * @param mixed $object Entity object instance
23
     * @return string Rendered entity field
24
     */
25
    public function render(RenderInterface $renderer, QueryInterface $query, $object)
26
    {
27
        $status = '';
28
        switch ($object->status) {
29
            case 0:
30
                $status = 'New';
31
                break;
32
            case 1:
33
                $status = 'Delivered';
34
                break;
35
            case 2:
36
                $status = 'Declined';
37
                break;
38
            case 3:
39
                $status = 'Deleted';
40
                break;
41
        }
42
        // Render input field view
43
        return $renderer
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface samsonframework\core\RenderInterface as the method view() does only exist in the following implementations of said interface: samson\activerecord\Module, samson\cms\CMS, samson\cms\table\TableConnector, samson\cms\web\field\FieldApplication, samson\cms\web\materialtable\App, samson\cms\web\navigation\StructureApplication, samson\cms\web\relatedmaterial\App, samson\core\CompressableExternalModule, samson\core\CompressableLocalModule, samson\core\CompressableService, samson\core\ExternalModule, samson\core\LocalModule, samson\core\Module, samson\core\Service, samson\core\System, samson\core\VirtualModule, samson\email\Email, samson\jquery\JQueryConnector, samson\jquery\JQueryUIConnector, samson\js\core\SamsonJSConnector, samson\js\lightbox\LightBox, samson\js\md5\SJSMD5Connector, samson\js\select\Select, samson\js\tabs\JSFixedHeader, samson\js\tabs\JSTranslit, samson\js\tinybox\TinyBox, samson\less\SamsonLessConnector, samson\parse\Parse, samson\resourcer\ResourceRouter, samson\social\Core, samson\social\email\Email, samson\treeview\TreeConnector, samsoncms\Application, samsoncms\api\CMS, samsoncms\app\gallery\Application, samsoncms\app\material\Application, samsoncms\app\security\Controller, samsoncms\app\signin\Application, samsoncms\app\user\Application, samsoncms\cms\Application, samsoncms\input\Application, samsoncms\input\bool\Application, samsoncms\input\date\Application, samsoncms\input\datetime\Application, samsoncms\input\file\Application, samsoncms\input\material\Application, samsoncms\input\number\Application, samsoncms\input\select\Application, samsoncms\input\text\Application, samsoncms\input\url\Application, samsoncms\input\wysiwyg\Application, samsoncms\newsletter\Application, samsoncms\seo\Core, samsoncms\template\Template, samsonphp\compressor\Controller, samsonphp\deploy\Deploy, samsonphp\fs\FileService, samsonphp\i18n\i18n, samsonphp\tests\TestingModule, samsonphp\upload\UploadController.

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...
44
            ->view($this->innerView)
45
            ->set('class', $this->css)
46
            ->set($object, 'item')
47
            ->set('status', $status)
48
            ->output();
49
    }
50
}
51