Completed
Branch master (9ffc9e)
by Pierre-Henry
35:33
created

framework/Layout/Form/Engine/PFBC/Element/Age.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * @author           Pierre-Henry Soria <[email protected]>
4
 * @copyright        (c) 2012-2018, Pierre-Henry Soria. All Rights Reserved.
5
 * @license          GNU General Public License; See PH7.LICENSE.txt and PH7.COPYRIGHT.txt in the root directory.
6
 * @link             http://ph7cms.com
7
 * @package          PH7 / Framework / Layout / Form / Engine / PFBC / Element
8
 */
9
10
namespace PFBC\Element;
11
12
use PFBC\OptionElement;
13
use PH7\Framework\Mvc\Model\DbConfig;
14
15
class Age extends OptionElement
16
{
17
    const MIN_AGE = 'min_age', MAX_AGE = 'max_age';
18
19
    /** @var string */
20
    protected $sHtmlOutput;
21
22
    /** @var int */
23
    protected $iMinAge;
24
25
    /** @var int */
26
    protected $iMaxAge;
27
28
    /**
29
     * Generate the select field for age search.
30
     *
31
     * @param array|null $aProperties
32
     */
33
    public function __construct($aProperties = null)
34
    {
35
        parent::__construct('', '', array(), $aProperties);
36
37
        $this->iMinAge = DbConfig::getSetting('minAgeRegistration');
0 ignored issues
show
Documentation Bug introduced by
It seems like \PH7\Framework\Mvc\Model...g('minAgeRegistration') can also be of type string or object<stdClass>. However, the property $iMinAge is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
38
        $this->iMaxAge = DbConfig::getSetting('maxAgeRegistration');
0 ignored issues
show
Documentation Bug introduced by
It seems like \PH7\Framework\Mvc\Model...g('maxAgeRegistration') can also be of type string or object<stdClass>. However, the property $iMaxAge is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
39
40
        $sSelect1 = static::getOptions(static::MIN_AGE);
41
        $sSelect2 = static::getOptions(static::MAX_AGE);
42
43
        $this->sHtmlOutput = '<div class="pfbc-label"><label><strong>*</strong>' . t('Age') . '</label></div><select name="age1">' . $sSelect1 . '</select> - <select name="age2">' . $sSelect2 . '</select> &nbsp; ' . t('years');
44
    }
45
46
    public function render()
47
    {
48
        echo $this->sHtmlOutput;
49
    }
50
51
    /**
52
     * @param string $sType 'min_age' or 'max_age'
53
     *
54
     * @return string The field age with the default selected minimum and maximum registration age.
55
     */
56
    protected function getOptions($sType)
57
    {
58
        $sSelect = '';
59
        $sAttrName = ($sType == static::MIN_AGE) ? 'iMinAge' : 'iMaxAge';
60
61
        for ($iAge = $this->iMinAge; $iAge <= $this->iMaxAge; $iAge++) {
62
            $sSelect .= '<option value="' . $iAge . '"';
63
64
            if (!empty($this->attributes['value'][$sType]) && $iAge == $this->attributes['value'][$sType]
65
                || empty($this->attributes['value'][$sType]) && $iAge == $this->$sAttrName
66
            ) {
67
                $sSelect .= ' selected="selected"';
68
            }
69
70
            $sSelect .= '>' . $iAge . '</option>';
71
        }
72
73
        return $sSelect;
74
    }
75
}
76