Completed
Push — master ( 2bc3ae...eaf16e )
by Mohamed
04:59
created

Moo_EditableFieldDate   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 37
ccs 0
cts 17
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getFieldConfiguration() 0 9 2
B populateFromPostData() 0 10 5
A initFormField() 0 8 2
1
<?php
2
3
/**
4
 * Moo_EditableFieldDate is an object representing date field created by CMS admin.
5
 *
6
 * @package editablefield
7
 *
8
 * @author  Mohamed Alsharaf <[email protected]>
9
 */
10
class Moo_EditableFieldDate extends Moo_EditableField
11
{
12
    private static $singular_name   = 'Date Field';
13
    private static $plural_name     = 'Date Fields';
14
    protected $customSettingsFields = [
15
        'DefaultToToday',
16
    ];
17
    public function getFieldConfiguration()
18
    {
19
        $default = ($this->getSetting('DefaultToToday')) ? $this->getSetting('DefaultToToday') : false;
20
        $label   = _t('Moo_EditableField.DEFAULTTOTODAY', 'Default to Today?');
21
22
        return [
23
            new CheckboxField($this->getSettingName('DefaultToToday'), $label, $default),
24
        ];
25
    }
26
27
    public function populateFromPostData($data)
28
    {
29
        $fieldPrefix = 'Default-';
30
31
        if (empty($data['Default']) && !empty($data[$fieldPrefix . 'Year']) && !empty($data[$fieldPrefix . 'Month']) && !empty($data[$fieldPrefix . 'Day'])) {
32
            $data['Default'] = $data['Year'] . '-' . $data['Month'] . '-' . $data['Day'];
33
        }
34
35
        parent::populateFromPostData($data);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Moo_EditableField as the method populateFromPostData() does only exist in the following sub-classes of Moo_EditableField: Moo_EditableFieldDate. 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...
36
    }
37
38
    protected function initFormField()
39
    {
40
        $defaultValue = ($this->getSetting('DefaultToToday')) ? date('Y-m-d') : $this->Default;
0 ignored issues
show
Bug introduced by
The property Default does not seem to exist. Did you mean default_cast?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
41
        $field        = new DateField($this->Name, $this->Title, $defaultValue);
42
        $field->setConfig('showcalendar', true);
43
44
        return $field;
45
    }
46
}
47