BookingForm   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 1
c 0
b 0
f 0
lcom 0
cbo 3
dl 0
loc 33
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 25 1
1
<?php
2
3
namespace JhFlexiTime\Form;
4
5
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
6
use Zend\Form\Form;
7
use Zend\InputFilter\InputFilter;
8
use Doctrine\Common\Persistence\ObjectManager;
9
use Zend\Form\FieldsetInterface;
10
11
/**
12
 * Class BookingForm
13
 * @package JhFlexiTime\Form
14
 * @author Aydin Hassan <[email protected]>
15
 */
16
class BookingForm extends Form
17
{
18
19
    /**
20
     * @param ObjectManager $objectManager
21
     * @param FieldsetInterface $bookingFieldset
22
     */
23
    public function __construct(ObjectManager $objectManager, FieldsetInterface $bookingFieldset)
24
    {
25
        parent::__construct('booking-form');
26
27
        $this->setHydrator(new DoctrineHydrator($objectManager, 'JhFlexiTime\Entity\Booking'))
0 ignored issues
show
Documentation introduced by
'JhFlexiTime\\Entity\\Booking' is of type string, but the function expects a boolean.

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...
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Form\FieldsetInterface as the method setInputFilter() does only exist in the following implementations of said interface: JhFlexiTime\Form\BookingForm, Zend\Form\Form, ZfcBase\Form\ProvidesEventsForm, ZfcUser\Form\Base, ZfcUser\Form\ChangeEmail, ZfcUser\Form\ChangePassword, ZfcUser\Form\Login, ZfcUser\Form\Register.

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...
28
            ->setInputFilter(new InputFilter())
29
            ->setAttribute('method', 'post')
30
            ->setAttribute('class', 'form-horizontal')
31
            ->setAttribute('id', 'booking-form');
32
33
        // Add the sample request fieldset
34
        $bookingFieldset->setUseAsBaseFieldset(true);
35
        $this->add($bookingFieldset);
36
37
        $this->add([
38
            'name' => 'submit',
39
            'attributes' => [
40
                'type' => 'submit',
41
                'value' => 'Submit',
42
                'id' => 'submitbutton',
43
                'class' => 'btn btn-danger',
44
            ]
45
        ]);
46
47
    }
48
}
49