FieldTextarea   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
lcom 1
cbo 5
dl 0
loc 37
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A fill() 0 11 1
A describe() 0 3 1
A setRequired() 0 5 1
1
<?php
2
3
namespace hemio\form;
4
5
use hemio\html;
6
7
class FieldTextarea extends Abstract_\FormFieldDefault {
8
9
    public function __construct($name, $title) {
10
        $this->init($name, $title, new html\Textarea());
11
    }
12
13
    /**
14
     *
15
     * @return \hemio\form\Abstract_\TemplateFormField
16
     */
17
    public function fill() {
18
        $template = $this->getFieldTemplateClone('TEXTAREA');
19
20
        $this['_TEMPLATE'] = $template;
21
        $template->init($this, $this->control);
22
        $this->control->addChild(new html\Str($this->getValueToUse()));
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface hemio\html\Interface_\Submittable as the method addChild() does only exist in the following implementations of said interface: hemio\html\Button, hemio\html\Keygen, hemio\html\Object, hemio\html\Select, hemio\html\Textarea.

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...
23
24
        $this->filled = true;
25
26
        return $template;
27
    }
28
29
    public function describe() {
30
        return 'INPUT(TEXTAREA)';
31
    }
32
33
    /**
34
     *
35
     * @param boolean $required
36
     */
37
    public function setRequired($required = true) {
38
        $this->addValidityCheck(new CheckMinLength(1));
39
        $this->required = $required;
40
        $this->control->setAttribute('required', (boolean) $required);
41
    }
42
43
}
44