GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 8b034b...b09e0d )
by Gabriel
07:03
created

FieldFormTrait   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 134
Duplicated Lines 21.64 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 19
c 0
b 0
f 0
lcom 1
cbo 2
dl 29
loc 134
ccs 0
cts 70
cp 0
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
B init() 0 25 3
A initVisibleElement() 7 7 1
A initMandatoryElement() 7 7 1
A hasListingFlags() 0 4 1
A getListingFlags() 0 4 1
A initListingElement() 7 7 1
getModelManager() 0 1 ?
A hasFilterFlags() 0 4 1
A getFilterFlags() 0 4 1
A initFilterElement() 7 7 1
A getDataFromModel() 0 17 4
A processValidation() 0 5 1
A saveToModel() 0 12 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace ByTIC\FormBuilder\Application\Modules\Admin\Forms\Traits;
4
5
use ByTIC\FormBuilder\Application\Models\Fields\Traits\FormFieldTrait;
6
use Nip\Records\Record;
7
use Nip\Records\RecordManager;
8
9
/**
10
 * Trait FieldFormTrait
11
 * @package ByTIC\FormBuilder\Application\Modules\Admin\Forms\Traits
12
 *
13
 * @method Record|FormFieldTrait getModel()
14
 * @method \Nip_Form_Element_Abstract getElement()
15
 */
16
trait FieldFormTrait
17
{
18
    public function init()
19
    {
20
        parent::init();
21
22
        $this->addSelect('type', translator()->translate('type'), true);
0 ignored issues
show
Bug introduced by
It seems like addSelect() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
23
        $this->getElement('type')->setAttrib('disabled', 'disabled');
0 ignored issues
show
Unused Code introduced by
The call to FieldFormTrait::getElement() has too many arguments starting with 'type'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
24
25
        $this->addInput('label', translator()->translate('name'), true);
0 ignored issues
show
Bug introduced by
It seems like addInput() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
26
27
        $this->addInput('label_intern', translator()->translate('internal_name'));
0 ignored issues
show
Bug introduced by
It seems like addInput() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
28
29
        $this->addInput('help', 'help');
0 ignored issues
show
Bug introduced by
It seems like addInput() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
30
31
        $this->initVisibleElement();
32
        $this->initMandatoryElement();
33
34
        if ($this->hasListingFlags()) {
35
            $this->initListingElement();
36
        }
37
        if ($this->hasFilterFlags()) {
38
            $this->initFilterElement();
39
        }
40
41
        $this->addButton('save', translator()->translate('submit'));
0 ignored issues
show
Bug introduced by
It seems like addButton() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
42
    }
43
44 View Code Duplication
    protected function initVisibleElement()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
    {
46
        $this->addBsRadioGroup('visible', translator()->translate('visible'), true);
0 ignored issues
show
Bug introduced by
It seems like addBsRadioGroup() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
47
        $this->visible->addOption('yes', translator()->translate('yes'))
0 ignored issues
show
Bug introduced by
The property visible does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
48
            ->addOption('no', translator()->translate('no'))
49
            ->getRenderer()->setSeparator('');
50
    }
51
52 View Code Duplication
    protected function initMandatoryElement()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
    {
54
        $this->addBsRadioGroup('mandatory', translator()->translate('mandatory'), true);
0 ignored issues
show
Bug introduced by
It seems like addBsRadioGroup() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
55
        $this->mandatory->addOption('yes', translator()->translate('yes'))
0 ignored issues
show
Bug introduced by
The property mandatory does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
56
            ->addOption('no', translator()->translate('no'))
57
            ->getRenderer()->setSeparator('');
58
    }
59
60
    /**
61
     * @return bool
62
     */
63
    protected function hasListingFlags()
64
    {
65
        return count($this->getListingFlags());
66
    }
67
68
    /**
69
     * @return array
70
     */
71
    protected function getListingFlags()
72
    {
73
        return [];
74
    }
75
76 View Code Duplication
    protected function initListingElement()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
    {
78
        $this->addCheckboxGroup('listing', $this->getModelManager()->getLabel('form.listing'), false);
0 ignored issues
show
Bug introduced by
It seems like addCheckboxGroup() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
79
        $this->listing->addOption('public', $this->getModelManager()->getLabel('form.listing.public'))
0 ignored issues
show
Bug introduced by
The property listing does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
80
            ->addOption('admin', $this->getModelManager()->getLabel('form.listing.admin'))
81
            ->getRenderer()->setSeparator('');
82
    }
83
84
    /**
85
     * @return RecordManager
86
     */
87
    abstract public function getModelManager();
88
89
    /**
90
     * @return bool
91
     */
92
    protected function hasFilterFlags()
93
    {
94
        return count($this->getFilterFlags());
95
    }
96
97
    /**
98
     * @return array
99
     */
100
    protected function getFilterFlags()
101
    {
102
        return [];
103
    }
104
105 View Code Duplication
    protected function initFilterElement()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
    {
107
        $this->addCheckboxGroup('filter', $this->getModelManager()->getLabel('form.filter'), false);
0 ignored issues
show
Bug introduced by
It seems like addCheckboxGroup() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
108
        $this->filter->addOption('public', $this->getModelManager()->getLabel('form.filter.public'))
0 ignored issues
show
Bug introduced by
The property filter does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
109
            ->addOption('admin', $this->getModelManager()->getLabel('form.filter.admin'))
110
            ->getRenderer()->setSeparator('');
111
    }
112
113
    public function getDataFromModel()
114
    {
115
        $fields = $this->getModel()->getManager()->getTypes();
0 ignored issues
show
Bug introduced by
The method getManager does only exist in Nip\Records\Record, but not in ByTIC\FormBuilder\Applic...s\Traits\FormFieldTrait.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
116
        foreach ($fields as $field) {
117
            $this->getElement('type')->addOption($field->getName(), $field->getLabel());
0 ignored issues
show
Unused Code introduced by
The call to FieldFormTrait::getElement() has too many arguments starting with 'type'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Nip_Form_Element_Abstract as the method addOption() does only exist in the following sub-classes of Nip_Form_Element_Abstract: Nip_Form_Element_CheckboxGroup, Nip_Form_Element_Input_Group, Nip_Form_Element_RadioGroup, Nip_Form_Element_Select. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
118
        }
119
120
        parent::getDataFromModel();
121
122
        if ($this->hasListingFlags()) {
123
            $this->getElement('listing')->setValue($this->getModel()->getListingArray());
0 ignored issues
show
Unused Code introduced by
The call to FieldFormTrait::getElement() has too many arguments starting with 'listing'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
124
        }
125
        if ($this->hasFilterFlags()) {
126
            $this->getElement('filter')->setValue($this->getModel()->getFilterArray());
0 ignored issues
show
Unused Code introduced by
The call to FieldFormTrait::getElement() has too many arguments starting with 'filter'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
127
        }
128
        $this->getModel()->getType()->adminGetDataFromModel($this);
0 ignored issues
show
Bug introduced by
The method getType does only exist in ByTIC\FormBuilder\Applic...s\Traits\FormFieldTrait, but not in Nip\Records\Record.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
129
    }
130
131
    public function processValidation()
132
    {
133
        parent::processValidation();
134
        $this->getModel()->getType()->adminProcessValidation($this);
0 ignored issues
show
Bug introduced by
The method getType does only exist in ByTIC\FormBuilder\Applic...s\Traits\FormFieldTrait, but not in Nip\Records\Record.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
135
    }
136
137
    public function saveToModel()
138
    {
139
        parent::saveToModel();
140
141
        if ($this->hasFilterFlags()) {
142
            $this->getModel()->listing = implode(',', $this->getElement('listing')->getValue());
0 ignored issues
show
Unused Code introduced by
The call to FieldFormTrait::getElement() has too many arguments starting with 'listing'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
143
        }
144
        if ($this->hasFilterFlags()) {
145
            $this->getModel()->filter = implode(',', $this->getElement('filter')->getValue());
0 ignored issues
show
Unused Code introduced by
The call to FieldFormTrait::getElement() has too many arguments starting with 'filter'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
146
        }
147
        $this->getModel()->getType()->adminSaveToModel($this);
0 ignored issues
show
Bug introduced by
The method getType does only exist in ByTIC\FormBuilder\Applic...s\Traits\FormFieldTrait, but not in Nip\Records\Record.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
148
    }
149
}
150