AncestryField   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 18
lcom 2
cbo 3
dl 0
loc 124
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A get_array_of_ancestors() 0 4 1
A set_array_of_ancestors() 0 4 1
A __construct() 0 7 2
A setForm() 0 7 2
A Field() 0 13 2
C setValue() 0 24 7
A dataValue() 0 8 2
A Icon() 0 4 1
1
<?php
2
/**
3
 *
4
 * @package userforms
5
 * @subpackage relatives
6
 */
7
class AncestryField 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
    //level 1
14
    protected static $array_of_ancestors = array(
15
        "mField" => "Mother",
16
        "fField" => "Father",
17
    //level 2
18
        "mmField" => "Mother's Mother",
19
        "mfField" => "Mother's Father",
20
        "fmField" => "Father's Mother",
21
        "ffField" => "Father's Father",
22
    //level 3
23
        "mmmField" => "Mother's Mother's Mother",
24
        "mmfField" => "Mother's Mother's Father",
25
        "mfmField" => "Mother's Father's Mother",
26
        "mffField" => "Mother's Father's Father",
27
        "fmmField" => "Father's Mother's Mother",
28
        "fmfField" => "Father's Mother's Father",
29
        "ffmField" => "Father's Father's Mother",
30
        "fffField" => "Father's Father's Father",
31
    //level 4
32
        "mmmmField" => "Mother's Mother's Mother's Mother",
33
        "mmmfField" => "Mother's Mother's Mother's Father",
34
        "mmfmField" => "Mother's Mother's Father's Mother",
35
        "mmffField" => "Mother's Mother's Father's Father",
36
        "mfmmField" => "Mother's Father's Mother's Mother",
37
        "mfmfField" => "Mother's Father's Mother's Father",
38
        "mffmField" => "Mother's Father's Father's Mother",
39
        "mfffField" => "Mother's Father's Father's Father",
40
        "fmmmField" => "Father's Mother's Mother's Mother",
41
        "fmmfField" => "Father's Mother's Mother's Father",
42
        "fmfmField" => "Father's Mother's Father's Mother",
43
        "fmffField" => "Father's Mother's Father's Father",
44
        "ffmmField" => "Father's Father's Mother's Mother",
45
        "ffmfField" => "Father's Father's Mother's Father",
46
        "fffmField" => "Father's Father's Father's Mother",
47
        "ffffField" => "Father's Father's Father's Father"
48
    );
49
    public static function get_array_of_ancestors()
50
    {
51
        return self::$array_of_ancestors;
52
    }
53
    public static function set_array_of_ancestors($a)
54
    {
55
        self::$array_of_ancestors = $a;
56
    }
57
58
    protected $fieldHolder = array();
59
60
    public function __construct($name, $title = null, $value = "")
61
    {
62
        foreach (self::get_array_of_ancestors() as $key => $fieldTitle) {
63
            $this->fieldHolder[$key] = new TextField($name . '['.$key.']', $fieldTitle);
64
        }
65
        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...
66
    }
67
68
    public function setForm($form)
69
    {
70
        parent::setForm($form);
71
        foreach (self::get_array_of_ancestors() as $key => $fieldTitle) {
72
            $this->fieldHolder[$key]->setForm($form);
73
        }
74
    }
75
76
    public function Field()
77
    {
78
        Requirements::themedCSS("AncestryField");
79
        Requirements::javascript(THIRDPARTY_DIR."/jquery/jquery.js");
80
        Requirements::javascript("userforms_relatives/javascript/AncestryField.js");
81
        $html = "";
82
        foreach (self::get_array_of_ancestors() as $key => $fieldTitle) {
83
            $levelClass = "level".(strlen($key)-5);
84
            $nextLevels = ".".str_replace("Field", "mField", $key).", .".str_replace("Field", "fField", $key);
85
            $html .= "<div class=\"$key $levelClass ancestorNode \" rel=\"$nextLevels\">".$this->fieldHolder[$key]->SmallFieldHolder()."</div>";
86
        }
87
        return $html;
88
    }
89
90
    /**
91
     */
92
    public function setValue($val)
93
    {
94
        if (empty($val)) {
95
            foreach (self::get_array_of_ancestors() as $key => $fieldTitle) {
96
                $this->fieldHolder[$key]->setValue(null);
97
            }
98
        } else {
99
            // String setting is only possible from the database, so we don't allow anything but ISO format
100
            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...
101
                //TO DO
102
            }
103
            // Setting from form submission
104
            elseif (is_array($val)) {
105
                foreach (self::get_array_of_ancestors() as $key => $fieldTitle) {
106
                    $myValue = isset($val[$key]) ? $val[$key] : "";
107
                    $this->fieldHolder[$key]->setValue($myValue);
108
                }
109
            } else {
110
                $this->nameField->setValue($val);
0 ignored issues
show
Documentation introduced by
The property nameField does not exist on object<AncestryField>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
111
                $this->dobField->setValue($val);
0 ignored issues
show
Documentation introduced by
The property dobField does not exist on object<AncestryField>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
112
                $this->sexField->setValue($val);
0 ignored issues
show
Documentation introduced by
The property sexField does not exist on object<AncestryField>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
113
            }
114
        }
115
    }
116
117
    public function dataValue()
118
    {
119
        $array = array();
120
        foreach (self::get_array_of_ancestors() as $key => $fieldTitle) {
121
            $array[$key] = $this->fieldHolder[$key]->dataValue();
122
        }
123
        return $array;
124
    }
125
126
    public function Icon()
127
    {
128
        return 'userforms/images/' . strtolower($this->class) . '.png';
129
    }
130
}
131