Gallery::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 2 Features 1
Metric Value
c 3
b 2
f 1
dl 0
loc 21
rs 9.3142
cc 3
eloc 12
nc 2
nop 4
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: onysko
5
 * Date: 10.06.2015
6
 * Time: 16:43
7
 */
8
9
namespace samsoncms\app\gallery\tab;
10
11
use samson\activerecord\field;
12
use samson\cms\Navigation;
13
use samsoncms\form\tab\Generic;
14
use samsonframework\core\RenderInterface;
15
use samsonframework\orm\QueryInterface;
16
use samsonframework\orm\Record;
17
18
/**
19
 * SamsonCMS material form gallery tab
20
 * @package samsoncms\app\gallery\tab
21
 */
22
class Gallery extends Generic
23
{
24
    /** @var string Tab name or identifier */
25
    protected $name = 'Галлерея';
26
27
    /** @var string HTML tab identifier*/
28
    protected $id = 'gallery_tab';
29
30
    /** @var \samson\activerecord\materialfield Pointer to gallery additional field record */
31
    public $materialField;
32
33
    /** @inheritdoc */
34
    public function __construct(RenderInterface $renderer, QueryInterface $query, Record $entity, field $field)
35
    {
36
        // Check if material have this gallery additional field stored
37
        if (!$query->className('materialfield')
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...
38
                ->cond('MaterialID', $entity->id)
39
                ->cond('FieldID', $field->id)
40
                ->first($this->materialField)) {
41
            // Create materialfield object
42
            $this->materialField = new \samson\activerecord\materialfield(false);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a null|object<samsonframew...\orm\DatabaseInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
43
            $this->materialField->FieldID = $field->id;
44
            $this->materialField->MaterialID = $entity->id;
45
            $this->materialField->Active = 1;
0 ignored issues
show
Documentation Bug introduced by
The property $Active was declared of type boolean, but 1 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
46
            $this->materialField->save();
47
        }
48
49
        // Form tab name
50
        $this->name = t(isset($field->Name{0}) ? $field->Name : $this->name, true);
0 ignored issues
show
Deprecated Code introduced by
The function t() has been deprecated with message: Should be used as $this->system->getContainer()->geti18n()->translate()|plural()

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...
51
52
        // Call parent constructor to define all class fields
53
        parent::__construct($renderer, $query, $entity);
54
    }
55
56
    /** @inheritdoc */
57
    public function content()
58
    {
59
        $content = $this->renderer->getHTML($this->materialField->id);
0 ignored issues
show
Bug introduced by
The method getHTML() does not seem to exist on object<samson\core\IViewable>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
60
61
        return $this->renderer->view($this->contentView)->content($content)->output();
0 ignored issues
show
Bug introduced by
The method content() does not seem to exist on object<samson\core\IViewable>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
62
    }
63
}
64