OffSpringField   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 5
dl 0
loc 151
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A setForm() 0 9 1
A Field() 0 6 1
C setValue() 0 23 7
A dataValue() 0 9 1
A getHeaderField() 0 4 1
A getNameField() 0 4 1
A getDobField() 0 4 1
A getSexField() 0 4 1
A validate() 0 8 3
A jsValidation() 0 4 1
A performReadonlyTransformation() 0 6 1
1
<?php
2
/**
3
 *
4
 * @package userforms
5
 * @subpackage relatives
6
 */
7
class OffSpringField extends FormField
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
8
{
9
10
    /**
11
     * @var nameField
12
     */
13
    protected $headerField = null;
14
15
    /**
16
     * @var nameField
17
     */
18
    protected $nameField = null;
19
20
    /**
21
     * @var dobField
22
     */
23
    protected $dobField = null;
24
25
    /**
26
     * @var dobField
27
     */
28
    protected $sexField = null;
29
30
31
    public function __construct($name, $title = null, $value = "")
32
    {
33
        $this->headerField = new HeaderField($name . '[header]', $title, 4);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \HeaderField($name . '[header]', $title, 4) of type object<HeaderField> is incompatible with the declared type object<nameField> of property $headerField.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
34
        $this->nameField = new TextField($name . '[name]', "Name");
0 ignored issues
show
Documentation Bug introduced by
It seems like new \TextField($name . '[name]', 'Name') of type object<TextField> is incompatible with the declared type object<nameField> of property $nameField.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
35
        $this->dobField = new TextField($name . '[dob]', "Date of Birth (e.g. 31/01/1986)");
0 ignored issues
show
Documentation Bug introduced by
It seems like new \TextField($name . '...rth (e.g. 31/01/1986)') of type object<TextField> is incompatible with the declared type object<dobField> of property $dobField.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
36
        $this->sexField = new OptionsetField($name . '[sex]', false, array("female", "male"));
0 ignored issues
show
Documentation introduced by
$name . '[sex]' is of type string, but the function expects a object<The>.

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...
Documentation introduced by
false is of type boolean, but the function expects a string|object<The>.

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...
Documentation Bug introduced by
It seems like new \OptionsetField($nam...rray('female', 'male')) of type object<OptionsetField> is incompatible with the declared type object<dobField> of property $sexField.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
37
        $this->headerField->addExtraClass("header");
38
        $this->nameField->addExtraClass("name");
39
        $this->dobField->addExtraClass("dob");
40
        $this->sexField->addExtraClass("sexField");
41
42
        parent::__construct($name, $title, $value);
0 ignored issues
show
Documentation introduced by
$value is of type string, but the function expects a object<The>|null.

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...
43
    }
44
45
    public function setForm($form)
46
    {
47
        parent::setForm($form);
48
49
        $this->headerField->setForm($form);
50
        $this->nameField->setForm($form);
51
        $this->dobField->setForm($form);
52
        $this->sexField->setForm($form);
53
    }
54
55
    public function Field()
56
    {
57
        //Requirements::css();
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
58
        //Requirements::javascript();
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
59
        return $this->headerField->SmallFieldHolder() . $this->nameField->SmallFieldHolder() . $this->dobField->SmallFieldHolder() . $this->sexField->SmallFieldHolder() . '<div class="clear"><!-- --></div>';
60
    }
61
62
    /**
63
     * Sets the internal value to ISO date format.
64
     *
65
     * @param string|array $val String expects an ISO date format. Array notation with 'date' and 'time'
66
     *  keys can contain localized strings. If the 'dmyfields' option is used for {@link nameField},
67
     *  the 'date' value may contain array notation was well (see {@link nameField->setValue()}).
68
     */
69
    public function setValue($val)
70
    {
71
        if (empty($val)) {
72
            $this->nameField->setValue(null);
73
            $this->dobField->setValue(null);
74
            $this->sexField->setValue(null);
75
        } else {
76
            // String setting is only possible from the database, so we don't allow anything but ISO format
77
            if (is_string($val)) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
78
                //TO DO
79
            }
80
            // Setting from form submission
81
            elseif (is_array($val) && array_key_exists('name', $val) && array_key_exists('dob', $val) && array_key_exists('sex', $val)) {
82
                $this->nameField->setValue($val['name']);
83
                $this->dobField->setValue($val['dob']);
84
                $this->sexField->setValue($val['sex']);
85
            } else {
86
                $this->nameField->setValue($val);
87
                $this->dobField->setValue($val);
88
                $this->sexField->setValue($val);
89
            }
90
        }
91
    }
92
93
    public function dataValue()
94
    {
95
        $valName = $this->nameField->dataValue();
96
        $valDob = $this->dobField->dataValue();
97
        $valSex = $this->sexField->dataValue();
98
99
        $array = array("name" => $valName, "dob" => $valDob, "sex" => $valSex);
100
        return implode("|", $array);
101
    }
102
103
104
    /**
105
     * @return nameField
106
     */
107
    public function getHeaderField()
108
    {
109
        return $this->headerField;
110
    }
111
112
    /**
113
     * @return nameField
114
     */
115
    public function getNameField()
116
    {
117
        return $this->nameField;
118
    }
119
120
    /**
121
     * @return dobField
122
     */
123
    public function getDobField()
124
    {
125
        return $this->dobField;
126
    }
127
128
    /**
129
     * @return sexField
0 ignored issues
show
Documentation introduced by
Should the return type not be dobField?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
130
     */
131
    public function getSexField()
132
    {
133
        return $this->sexField;
134
    }
135
136
137
    public function validate($validator)
138
    {
139
        $dateValid = $this->nameField->validate($validator);
140
        $timeValid = $this->dobField->validate($validator);
141
        $sexValid = $this->sexField->validate($validator);
142
143
        return ($dateValid && $timeValid && $sexValid);
144
    }
145
146
    public function jsValidation()
147
    {
148
        return $this->nameField->jsValidation() . $this->dobField->jsValidation() . $this->sexField->jsValidation() ;
149
    }
150
151
    public function performReadonlyTransformation()
152
    {
153
        $field = new OffSpringField_Readonly($this->name, $this->title, $this->dataValue());
154
        $field->setForm($this->form);
155
        return $field;
156
    }
157
}
158
159
/**
160
161
 */
162
class OffSpringField_Readonly extends OffSpringField
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
163
{
164
    protected $readonly = true;
165
166
    public function Field()
167
    {
168
        $valName = $this->nameField->dataValue();
169
        $valDob = $this->dobField->dataValue();
170
        $valSex = $this->sexField->dataValue();
171
        if ($valDate && $valTime && $valSex) {
0 ignored issues
show
Bug introduced by
The variable $valDate does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $valTime does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
172
            $val = $valName." ".$valDob." ".$valSex;
173
        } else {
174
            // TODO Localization
175
            $val = '<i>(not set)</i>';
176
        }
177
178
        return "<span class=\"readonly\" id=\"" . $this->id() . "\">$val</span>";
179
    }
180
181
    public function jsValidation()
182
    {
183
        return null;
184
    }
185
186
    public function validate($validator)
187
    {
188
        return true;
189
    }
190
}
191