Skip to content

Commit

Permalink
Add PHPDoc return types
Browse files Browse the repository at this point in the history
Symfony PHPUnit Bridge has a deprecation helper that warns when things might break in the future.

It currently reports the following errors when using Stripe PHP SDK:

```
Method "ArrayAccess::offsetExists()" might add "bool" as a native return type declaration in the future. Do the same in implementation "Stripe\StripeObject" now to avoid errors or add an explicit @return annotation to suppress this message.
Method "ArrayAccess::offsetGet()" might add "mixed" as a native return type declaration in the future. Do the same in implementation "Stripe\StripeObject" now to avoid errors or add an explicit @return annotation to suppress this message.
Method "ArrayAccess::offsetSet()" might add "void" as a native return type declaration in the future. Do the same in implementation "Stripe\StripeObject" now to avoid errors or add an explicit @return annotation to suppress this message.
Method "ArrayAccess::offsetUnset()" might add "void" as a native return type declaration in the future. Do the same in implementation "Stripe\StripeObject" now to avoid errors or add an explicit @return annotation to suppress this message.
Method "Countable::count()" might add "int" as a native return type declaration in the future. Do the same in implementation "Stripe\StripeObject" now to avoid errors or add an explicit @return annotation to suppress this message.
Method "JsonSerializable::jsonSerialize()" might add "mixed" as a native return type declaration in the future. Do the same in implementation "Stripe\StripeObject" now to avoid errors or add an explicit @return annotation to suppress this message.
```

By adding these PHPDoc types, the problem disappears. It's also more clear for users of the package.

Fixes #1210
  • Loading branch information
ruudk committed Jan 4, 2022
1 parent f70cb95 commit d04316f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@
// but we can feel free to remove it in a major version (or maybe in a minor if
// we devote some effort into determining that it is safe)
'no_null_property_initialization' => false,

// We have to support `@return void` to satisfy Symfony deprecations helper.
// See https://github.com/stripe/stripe-php/pull/1230
'phpdoc_no_empty_return' => false,
]);
$config->setFinder($finder);
return $config;
7 changes: 6 additions & 1 deletion lib/StripeObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@ public function offsetGet($k)
return \array_key_exists($k, $this->_values) ? $this->_values[$k] : null;
}

// Countable method
/**
* @return int
*/
#[\ReturnTypeWillChange]
public function count()
{
Expand Down Expand Up @@ -424,6 +426,9 @@ public function serializeParamsValue($value, $original, $unsaved, $force, $key =
}
}

/**
* @return mixed
*/
#[\ReturnTypeWillChange]
public function jsonSerialize()
{
Expand Down
15 changes: 15 additions & 0 deletions lib/Util/CaseInsensitiveArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,18 @@ public function count()
return \count($this->container);
}

/**
* @return \ArrayIterator
*/
#[\ReturnTypeWillChange]
public function getIterator()
{
return new \ArrayIterator($this->container);
}

/**
* @return void
*/
#[\ReturnTypeWillChange]
public function offsetSet($offset, $value)
{
Expand All @@ -44,6 +50,9 @@ public function offsetSet($offset, $value)
}
}

/**
* @return bool
*/
#[\ReturnTypeWillChange]
public function offsetExists($offset)
{
Expand All @@ -52,13 +61,19 @@ public function offsetExists($offset)
return isset($this->container[$offset]);
}

/**
* @return void
*/
#[\ReturnTypeWillChange]
public function offsetUnset($offset)
{
$offset = static::maybeLowercase($offset);
unset($this->container[$offset]);
}

/**
* @return mixed
*/
#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
Expand Down
3 changes: 3 additions & 0 deletions lib/Util/Set.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public function toArray()
return \array_keys($this->_elts);
}

/**
* @return ArrayIterator
*/
#[\ReturnTypeWillChange]
public function getIterator()
{
Expand Down

0 comments on commit d04316f

Please sign in to comment.