Completed
Pull Request — master (#332)
by Claus
02:05
created

DeprecatedViewHelper::resetState()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace TYPO3Fluid\Fluid\Core\ViewHelper\Traits;
3
4
/**
5
 * Class DeprecatedViewHelper
6
 *
7
 * Contains methods which are deprecated and scheduled for removal.
8
 */
9
trait DeprecatedViewHelper
10
{
11
    /**
12
     * ViewHelper Variable Container
13
     * @var ViewHelperVariableContainer
14
     * @deprecated Will be removed in Fluid 3.0; use $renderingContext->getViewHelperVariableContainer() instead.
15
     * @api
16
     */
17
    protected $viewHelperVariableContainer;
18
19
    /**
20
     * Current variable container reference.
21
     * @var VariableProviderInterface
22
     * @deprecated Will be removed in Fluid 3.0; use $renderingContext->getVariableProvider() instead.
23
     * @api
24
     */
25
    protected $templateVariableContainer;
26
27
    /**
28
     * @var NodeInterface[] array
29
     * @api
30
     * @deprecated Will be removed in Fluid 3.0; use $this->viewHelperNode->getChildNodes() instead.
31
     */
32
    protected $childNodes = [];
33
34
    /**
35
     * Overridden method which sets deprecated properties.
36
     *
37
     * @param RenderingContextInterface $renderingContext
38
     * @return void
39
     */
40
    public function setRenderingContext(RenderingContextInterface $renderingContext)
41
    {
42
        parent::setRenderingContext($renderingContext);
43
        $this->templateVariableContainer = $renderingContext->getVariableProvider();
0 ignored issues
show
Deprecated Code introduced by
The property TYPO3Fluid\Fluid\Core\Vi...mplateVariableContainer has been deprecated with message: Will be removed in Fluid 3.0; use $renderingContext->getVariableProvider() instead.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
44
        $this->viewHelperVariableContainer = $renderingContext->getViewHelperVariableContainer();
0 ignored issues
show
Deprecated Code introduced by
The property TYPO3Fluid\Fluid\Core\Vi...HelperVariableContainer has been deprecated with message: Will be removed in Fluid 3.0; use $renderingContext->getViewHelperVariableContainer() instead.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
45
    }
46
47
    /**
48
     * DEPRECATED - is no longer called.
49
     *
50
     * This is PURELY INTERNAL! Never override this method!!
51
     *
52
     * @deprecated
53
     * @param NodeInterface[] $childNodes
54
     * @return void
55
     */
56
    public function setChildNodes(array $childNodes)
57
    {
58
        $this->childNodes = $childNodes;
0 ignored issues
show
Deprecated Code introduced by
The property TYPO3Fluid\Fluid\Core\Vi...ViewHelper::$childNodes has been deprecated with message: Will be removed in Fluid 3.0; use $this->viewHelperNode->getChildNodes() instead.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
59
    }
60
61
    /**
62
     * DEPRECATED - is no longer called.
63
     *
64
     * Resets the ViewHelper state.
65
     *
66
     * Overwrite this method if you need to get a clean state of your ViewHelper.
67
     *
68
     * @deprecated
69
     * @return void
70
     */
71
    public function resetState()
72
    {
73
    }
74
75
    /**
76
     * DEPRECATED - is no longer called.
77
     *
78
     * Initializes the ViewHelper. Deprecated since frameworks by the norm provide object lifecycle methods.
79
     *
80
     * @deprecated
81
     * @return void
82
     */
83
    public function initialize()
84
    {
85
    }
86
87
    /**
88
     * DEPRECATED - is no longer called; handled by ViewHelperArgumentValidator
89
     *
90
     * Validate arguments, and throw exception if arguments do not validate.
91
     *
92
     * @deprecated
93
     * @return void
94
     * @throws \InvalidArgumentException
95
     */
96
    public function validateArguments()
97
    {
98
        $argumentDefinitions = $this->prepareArguments();
0 ignored issues
show
Bug introduced by
It seems like prepareArguments() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
99
        foreach ($argumentDefinitions as $argumentName => $registeredArgument) {
100
            if ($this->hasArgument($argumentName)) {
0 ignored issues
show
Bug introduced by
It seems like hasArgument() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
101
                $value = $this->arguments[$argumentName];
0 ignored issues
show
Bug introduced by
The property arguments does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
102
                $type = $registeredArgument->getType();
103
                if ($value !== $registeredArgument->getDefaultValue() && $type !== 'mixed') {
104
                    $givenType = is_object($value) ? get_class($value) : gettype($value);
105
                    if (!$this->isValidType($type, $value)) {
106
                        throw new \InvalidArgumentException(
107
                            'The argument "' . $argumentName . '" was registered with type "' . $type . '", but is of type "' .
108
                            $givenType . '" in view helper "' . get_class($this) . '".',
109
                            1256475113
110
                        );
111
                    }
112
                }
113
            }
114
        }
115
    }
116
117
    /**
118
     * Check whether the defined type matches the value type
119
     *
120
     * @param string $type
121
     * @param mixed $value
122
     * @return boolean
123
     */
124
    protected function isValidType($type, $value)
125
    {
126
        if ($type === 'object') {
127
            if (!is_object($value)) {
128
                return false;
129
            }
130
        } elseif ($type === 'array' || substr($type, -2) === '[]') {
131
            if (!is_array($value) && !$value instanceof \ArrayAccess && !$value instanceof \Traversable && !empty($value)) {
132
                return false;
133
            } elseif (substr($type, -2) === '[]') {
134
                $firstElement = $this->getFirstElementOfNonEmpty($value);
135
                if ($firstElement === null) {
136
                    return true;
137
                }
138
                return $this->isValidType(substr($type, 0, -2), $firstElement);
139
            }
140
        } elseif ($type === 'string') {
141
            if (is_object($value) && !method_exists($value, '__toString')) {
142
                return false;
143
            }
144
        } elseif ($type === 'boolean' && !is_bool($value)) {
145
            return false;
146
        } elseif (class_exists($type) && $value !== null && !$value instanceof $type) {
147
            return false;
148
        } elseif (is_object($value) && !is_a($value, $type, true)) {
149
            return false;
150
        }
151
        return true;
152
    }
153
154
    /**
155
     * Return the first element of the given array, ArrayAccess or Traversable
156
     * that is not empty
157
     *
158
     * @param mixed $value
159
     * @return mixed
160
     */
161
    protected function getFirstElementOfNonEmpty($value)
162
    {
163
        if (is_array($value)) {
164
            return reset($value);
165
        } elseif ($value instanceof \Traversable) {
166
            foreach ($value as $element) {
167
                return $element;
168
            }
169
        }
170
        return null;
171
    }
172
173
}
174