FormGroup::render()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 23
rs 9.0856
cc 2
eloc 17
nc 2
nop 3
1
<?php
2
namespace samsoncms\app\user\field;
3
4
use samsonframework\core\RenderInterface;
5
use samsonframework\orm\QueryInterface;
6
use samsoncms\form\field\Generic;
7
8
/**
9
 * Overridden group field
10
 * @package samsoncms\app\user
11
 */
12
class FormGroup extends Generic
13
{
14
    /** @inheritdoc */
15
    public function render(RenderInterface $renderer, QueryInterface $query, $object)
16
    {
17
        // Get all available groups from db
18
        $groupSelect = array();
19
        foreach ($query->className('group')->cond('Active', 1)->exec() as $group) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface samsonframework\orm\QueryInterface as the method className() does only exist in the following implementations of said interface: samson\activerecord\dbQuery.

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...
20
            $groupSelect[] = $group->GroupID.':'.$group->Name;
21
        }
22
23
        // Set view
24
        return $renderer->view($this->innerView)
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\core\CompressableExternalModule, samson\core\CompressableService, samson\core\ExternalModule, samson\core\Service, samson\core\VirtualModule, samson\js\core\SamsonJSConnector, samson\js\md5\SJSMD5Connector, samson\js\select\Select, samson\parse\Parse, samson\social\Core, samson\social\email\Email, samsoncms\Application, samsoncms\api\CMS, samsoncms\app\user\Application, samsoncms\input\Application, samsoncms\input\password\Application, samsoncms\template\Template, samsonjavascript\lightbox\LightBox, samsonjavascript\tinybox\TinyBox, samsonphp\compressor\Controller, samsonphp\core\Module, samsonphp\core\System, samsonphp\deploy\Deploy, samsonphp\i18n\i18n, samsonphp\less\Module, samsonphp\resource\Router, samsonphp\view\Module.

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...
25
            ->set($this->css, 'class')
26
            ->set($this->title, 'title')
27
            ->set($object, 'item')
28
            ->set(
29
                m('samsoncms_input_application')->createFieldByType(
0 ignored issues
show
Deprecated Code introduced by
The function m() has been deprecated with message: Use $this->system->module() in module context

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
30
                    $query,
31
                    4,
32
                    $object,
33
                    $this->name
34
                )->build(implode(',', $groupSelect)),
35
                'field'
36
            )->output();
37
    }
38
}