Skip to content

Commit e5adbe6

Browse files
committed
minor #32234 [PropertyAccess] Add type-hints to public interfaces and classes (jschaedl)
This PR was squashed before being merged into the 5.0-dev branch (closes #32234). Discussion ---------- [PropertyAccess] Add type-hints to public interfaces and classes | Q | A | ------------- | --- | Branch? | master <!-- see below --> | Bug fix? | no | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | #32179 <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | N/A <!-- required for new features --> This PR adds type hints to public interfaces and classes of the PropertyAccess component. Some docblocks without valuable information got also removed. Commits ------- 91138a9 [PropertyAccess] Add type-hints to public interfaces and classes
2 parents 7eb0912 + 91138a9 commit e5adbe6

File tree

5 files changed

+27
-30
lines changed

5 files changed

+27
-30
lines changed

UPGRADE-5.0.md

+5
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,11 @@ Process
336336
$process = Process::fromShellCommandline('ls -l');
337337
```
338338

339+
PropertyAccess
340+
--------------
341+
342+
* Removed support of passing `null` as 2nd argument of `PropertyAccessor::createCache()` method (`$defaultLifetime`), pass `0` instead.
343+
339344
Routing
340345
-------
341346

src/Symfony/Component/PropertyAccess/PropertyAccessor.php

+8-16
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public function setValue(&$objectOrArray, $propertyPath, $value)
181181
}
182182
}
183183

184-
private static function throwInvalidArgumentException($message, $trace, $i, $propertyPath)
184+
private static function throwInvalidArgumentException(string $message, array $trace, $i, $propertyPath)
185185
{
186186
// the type mismatch is not caused by invalid arguments (but e.g. by an incompatible return type hint of the writer method)
187187
if (0 !== strpos($message, 'Argument ')) {
@@ -274,7 +274,7 @@ public function isWritable($objectOrArray, $propertyPath)
274274
* @throws UnexpectedTypeException if a value within the path is neither object nor array
275275
* @throws NoSuchIndexException If a non-existing index is accessed
276276
*/
277-
private function readPropertiesUntil($zval, PropertyPathInterface $propertyPath, $lastIndex, $ignoreInvalidIndices = true)
277+
private function readPropertiesUntil(array $zval, PropertyPathInterface $propertyPath, int $lastIndex, bool $ignoreInvalidIndices = true)
278278
{
279279
if (!\is_object($zval[self::VALUE]) && !\is_array($zval[self::VALUE])) {
280280
throw new UnexpectedTypeException($zval[self::VALUE], $propertyPath, 0);
@@ -383,7 +383,7 @@ private function readIndex($zval, $index)
383383
*
384384
* @throws NoSuchPropertyException If $ignoreInvalidProperty is false and the property does not exist or is not public
385385
*/
386-
private function readProperty($zval, $property, bool $ignoreInvalidProperty = false)
386+
private function readProperty(array $zval, string $property, bool $ignoreInvalidProperty = false)
387387
{
388388
if (!\is_object($zval[self::VALUE])) {
389389
throw new NoSuchPropertyException(sprintf('Cannot read property "%s" from an array. Maybe you intended to write the property path as "[%1$s]" instead.', $property));
@@ -430,12 +430,9 @@ private function readProperty($zval, $property, bool $ignoreInvalidProperty = fa
430430
/**
431431
* Guesses how to read the property value.
432432
*
433-
* @param string $class
434-
* @param string $property
435-
*
436433
* @return array
437434
*/
438-
private function getReadAccessInfo($class, $property)
435+
private function getReadAccessInfo(string $class, string $property)
439436
{
440437
$key = str_replace('\\', '.', $class).'..'.$property;
441438

@@ -520,7 +517,7 @@ private function getReadAccessInfo($class, $property)
520517
*
521518
* @throws NoSuchIndexException If the array does not implement \ArrayAccess or it is not an array
522519
*/
523-
private function writeIndex($zval, $index, $value)
520+
private function writeIndex(array $zval, $index, $value)
524521
{
525522
if (!$zval[self::VALUE] instanceof \ArrayAccess && !\is_array($zval[self::VALUE])) {
526523
throw new NoSuchIndexException(sprintf('Cannot modify index "%s" in object of type "%s" because it doesn\'t implement \ArrayAccess.', $index, \get_class($zval[self::VALUE])));
@@ -538,7 +535,7 @@ private function writeIndex($zval, $index, $value)
538535
*
539536
* @throws NoSuchPropertyException if the property does not exist or is not public
540537
*/
541-
private function writeProperty($zval, $property, $value)
538+
private function writeProperty(array $zval, string $property, $value)
542539
{
543540
if (!\is_object($zval[self::VALUE])) {
544541
throw new NoSuchPropertyException(sprintf('Cannot write property "%s" to an array. Maybe you should write the property path as "[%1$s]" instead?', $property));
@@ -579,7 +576,7 @@ private function writeProperty($zval, $property, $value)
579576
* @param string $addMethod The add*() method
580577
* @param string $removeMethod The remove*() method
581578
*/
582-
private function writeCollection($zval, $property, $collection, $addMethod, $removeMethod)
579+
private function writeCollection(array $zval, string $property, $collection, string $addMethod, string $removeMethod)
583580
{
584581
// At this point the add and remove methods have been found
585582
$previousValue = $this->readProperty($zval, $property);
@@ -817,16 +814,11 @@ private function getPropertyPath($propertyPath): PropertyPath
817814
/**
818815
* Creates the APCu adapter if applicable.
819816
*
820-
* @param string $namespace
821-
* @param int $defaultLifetime
822-
* @param string $version
823-
* @param LoggerInterface|null $logger
824-
*
825817
* @return AdapterInterface
826818
*
827819
* @throws \LogicException When the Cache Component isn't available
828820
*/
829-
public static function createCache($namespace, $defaultLifetime, $version, LoggerInterface $logger = null)
821+
public static function createCache(string $namespace, int $defaultLifetime, string $version, LoggerInterface $logger = null)
830822
{
831823
if (null === $defaultLifetime) {
832824
@trigger_error(sprintf('Passing null as "$defaultLifetime" 2nd argument of the "%s()" method is deprecated since Symfony 4.4, pass 0 instead.', __METHOD__), E_USER_DEPRECATED);

src/Symfony/Component/PropertyAccess/PropertyPath.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public function getElements()
170170
/**
171171
* {@inheritdoc}
172172
*/
173-
public function getElement($index)
173+
public function getElement(int $index)
174174
{
175175
if (!isset($this->elements[$index])) {
176176
throw new OutOfBoundsException(sprintf('The index %s is not within the property path', $index));
@@ -182,7 +182,7 @@ public function getElement($index)
182182
/**
183183
* {@inheritdoc}
184184
*/
185-
public function isProperty($index)
185+
public function isProperty(int $index)
186186
{
187187
if (!isset($this->isIndex[$index])) {
188188
throw new OutOfBoundsException(sprintf('The index %s is not within the property path', $index));
@@ -194,7 +194,7 @@ public function isProperty($index)
194194
/**
195195
* {@inheritdoc}
196196
*/
197-
public function isIndex($index)
197+
public function isIndex(int $index)
198198
{
199199
if (!isset($this->isIndex[$index])) {
200200
throw new OutOfBoundsException(sprintf('The index %s is not within the property path', $index));

src/Symfony/Component/PropertyAccess/PropertyPathBuilder.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function __construct($path = null)
4343
* @param int $length The length of the appended piece
4444
* If 0, the full path is appended
4545
*/
46-
public function append($path, $offset = 0, $length = 0)
46+
public function append($path, int $offset = 0, int $length = 0)
4747
{
4848
if (\is_string($path)) {
4949
$path = new PropertyPath($path);
@@ -66,7 +66,7 @@ public function append($path, $offset = 0, $length = 0)
6666
*
6767
* @param string $name The name of the appended index
6868
*/
69-
public function appendIndex($name)
69+
public function appendIndex(string $name)
7070
{
7171
$this->elements[] = $name;
7272
$this->isIndex[] = true;
@@ -77,7 +77,7 @@ public function appendIndex($name)
7777
*
7878
* @param string $name The name of the appended property
7979
*/
80-
public function appendProperty($name)
80+
public function appendProperty(string $name)
8181
{
8282
$this->elements[] = $name;
8383
$this->isIndex[] = false;
@@ -91,7 +91,7 @@ public function appendProperty($name)
9191
*
9292
* @throws OutOfBoundsException if offset is invalid
9393
*/
94-
public function remove($offset, $length = 1)
94+
public function remove(int $offset, int $length = 1)
9595
{
9696
if (!isset($this->elements[$offset])) {
9797
throw new OutOfBoundsException(sprintf('The offset %s is not within the property path', $offset));
@@ -113,7 +113,7 @@ public function remove($offset, $length = 1)
113113
*
114114
* @throws OutOfBoundsException If the offset is invalid
115115
*/
116-
public function replace($offset, $length, $path, $pathOffset = 0, $pathLength = 0)
116+
public function replace(int $offset, int $length, $path, int $pathOffset = 0, int $pathLength = 0)
117117
{
118118
if (\is_string($path)) {
119119
$path = new PropertyPath($path);
@@ -146,7 +146,7 @@ public function replace($offset, $length, $path, $pathOffset = 0, $pathLength =
146146
*
147147
* @throws OutOfBoundsException If the offset is invalid
148148
*/
149-
public function replaceByIndex($offset, $name = null)
149+
public function replaceByIndex(int $offset, string $name = null)
150150
{
151151
if (!isset($this->elements[$offset])) {
152152
throw new OutOfBoundsException(sprintf('The offset %s is not within the property path', $offset));
@@ -167,7 +167,7 @@ public function replaceByIndex($offset, $name = null)
167167
*
168168
* @throws OutOfBoundsException If the offset is invalid
169169
*/
170-
public function replaceByProperty($offset, $name = null)
170+
public function replaceByProperty(int $offset, string $name = null)
171171
{
172172
if (!isset($this->elements[$offset])) {
173173
throw new OutOfBoundsException(sprintf('The offset %s is not within the property path', $offset));
@@ -233,7 +233,7 @@ public function __toString()
233233
* @param int $cutLength The length of the removed chunk
234234
* @param int $insertionLength The length of the inserted chunk
235235
*/
236-
private function resize($offset, $cutLength, $insertionLength)
236+
private function resize(int $offset, int $cutLength, int $insertionLength)
237237
{
238238
// Nothing else to do in this case
239239
if ($insertionLength === $cutLength) {

src/Symfony/Component/PropertyAccess/PropertyPathInterface.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function getElements();
6060
*
6161
* @throws Exception\OutOfBoundsException If the offset is invalid
6262
*/
63-
public function getElement($index);
63+
public function getElement(int $index);
6464

6565
/**
6666
* Returns whether the element at the given index is a property.
@@ -71,7 +71,7 @@ public function getElement($index);
7171
*
7272
* @throws Exception\OutOfBoundsException If the offset is invalid
7373
*/
74-
public function isProperty($index);
74+
public function isProperty(int $index);
7575

7676
/**
7777
* Returns whether the element at the given index is an array index.
@@ -82,5 +82,5 @@ public function isProperty($index);
8282
*
8383
* @throws Exception\OutOfBoundsException If the offset is invalid
8484
*/
85-
public function isIndex($index);
85+
public function isIndex(int $index);
8686
}

0 commit comments

Comments
 (0)