Skip to content

Commit

Permalink
Require stubs to declare return types for magic methods when possible
Browse files Browse the repository at this point in the history
Closes GH-6376
  • Loading branch information
kocsismate committed Oct 23, 2020
1 parent 0fb2374 commit 47bbfe1
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 163 deletions.
10 changes: 7 additions & 3 deletions Zend/zend_exceptions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ public function getTraceAsString(): string;

class Exception implements Throwable
{
final private function __clone() {}
final private function __clone(): void {}

public function __construct(string $message = "", int $code = 0, ?Throwable $previous = null) {}

/** @return void */
public function __wakeup() {}

final public function getMessage(): string {}
Expand Down Expand Up @@ -56,12 +57,15 @@ final public function getSeverity(): int {}
class Error implements Throwable
{
/** @implementation-alias Exception::__clone */
final private function __clone() {}
final private function __clone(): void {}

/** @implementation-alias Exception::__construct */
public function __construct(string $message = "", int $code = 0, ?Throwable $previous = null) {}

/** @implementation-alias Exception::__wakeup */
/**
* @return void
* @implementation-alias Exception::__wakeup
*/
public function __wakeup() {}

/** @implementation-alias Exception::getMessage */
Expand Down
7 changes: 4 additions & 3 deletions Zend/zend_exceptions_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: bc49b326136997660887b12f0c59f8a57b17ecaf */
* Stub hash: 3699b51b31e509c11435845c7e0d35a2608dd268 */

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Throwable_getMessage, 0, 0, IS_STRING, 0)
ZEND_END_ARG_INFO()
Expand All @@ -20,7 +20,8 @@ ZEND_END_ARG_INFO()

#define arginfo_class_Throwable_getTraceAsString arginfo_class_Throwable_getMessage

#define arginfo_class_Exception___clone arginfo_class_Throwable_getCode
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Exception___clone, 0, 0, IS_VOID, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Exception___construct, 0, 0, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, message, IS_STRING, 0, "\"\"")
Expand Down Expand Up @@ -57,7 +58,7 @@ ZEND_END_ARG_INFO()

#define arginfo_class_ErrorException_getSeverity arginfo_class_Throwable_getLine

#define arginfo_class_Error___clone arginfo_class_Throwable_getCode
#define arginfo_class_Error___clone arginfo_class_Exception___clone

#define arginfo_class_Error___construct arginfo_class_Exception___construct

Expand Down
18 changes: 9 additions & 9 deletions build/gen_stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,9 @@ interface FunctionOrMethodName {
public function getDeclaration(): string;
public function getArgInfoName(): string;
public function __toString(): string;
public function isMagicMethod(): bool;
public function isMethod(): bool;
public function isConstructor(): bool;
public function isDestructor(): bool;
}

class FunctionName implements FunctionOrMethodName {
Expand Down Expand Up @@ -403,15 +403,15 @@ public function __toString(): string {
return $this->name->toString();
}

public function isMagicMethod(): bool {
public function isMethod(): bool {
return false;
}

public function isMethod(): bool {
public function isConstructor(): bool {
return false;
}

public function isConstructor(): bool {
public function isDestructor(): bool {
return false;
}
}
Expand Down Expand Up @@ -443,17 +443,17 @@ public function __toString(): string {
return "$this->className::$this->methodName";
}

public function isMagicMethod(): bool {
return strpos($this->methodName, '__') === 0;
}

public function isMethod(): bool {
return true;
}

public function isConstructor(): bool {
return $this->methodName === "__construct";
}

public function isDestructor(): bool {
return $this->methodName === "__destruct";
}
}

class ReturnInfo {
Expand Down Expand Up @@ -879,7 +879,7 @@ function parseFunctionLike(
}

$returnType = $func->getReturnType();
if ($returnType === null && !$haveDocReturnType && !$name->isMagicMethod()) {
if ($returnType === null && !$haveDocReturnType && !$name->isConstructor() && !$name->isDestructor()) {
throw new Exception("Missing return type for function $name()");
}

Expand Down
6 changes: 6 additions & 0 deletions ext/date/php_date.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,15 @@ public function getTimestamp();
/** @return DateInterval|false */
public function diff(DateTimeInterface $targetObject, bool $absolute = false);

/** @return void */
public function __wakeup();
}

class DateTime implements DateTimeInterface
{
public function __construct(string $datetime = "now", ?DateTimeZone $timezone = null) {}

/** @return void */
public function __wakeup() {}

/** @return DateTime */
Expand Down Expand Up @@ -246,6 +248,7 @@ class DateTimeImmutable implements DateTimeInterface
{
public function __construct(string $datetime = "now", ?DateTimeZone $timezone = null) {}

/** @return void */
public function __wakeup() {}

/** @return DateTimeZone */
Expand Down Expand Up @@ -363,6 +366,7 @@ public static function listAbbreviations() {}
*/
public static function listIdentifiers(int $timezoneGroup = DateTimeZone::ALL, ?string $countryCode = null) {}

/** @return void */
public function __wakeup() {}

/** @return DateTimeZone */
Expand All @@ -385,6 +389,7 @@ public static function createFromDateString(string $datetime) {}
*/
public function format(string $format) {}

/** @return void */
public function __wakeup() {}

/** @return DateInterval */
Expand Down Expand Up @@ -413,6 +418,7 @@ public function getDateInterval() {}
/** @return int|null */
public function getRecurrences() {}

/** @return void */
public function __wakeup() {}

/** @return DatePeriod */
Expand Down
2 changes: 1 addition & 1 deletion ext/date/php_date_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 04954b7aac5b3ee8e789b3ddd254ad5eda299c2b */
* Stub hash: 07d0e6179d838847d4a5d454f8b56438f8df19c0 */

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_strtotime, 0, 1, MAY_BE_LONG|MAY_BE_FALSE)
ZEND_ARG_TYPE_INFO(0, datetime, IS_STRING, 0)
Expand Down
20 changes: 10 additions & 10 deletions ext/reflection/php_reflection.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ interface Reflector extends Stringable
abstract class ReflectionFunctionAbstract implements Reflector
{
/** @implementation-alias ReflectionClass::__clone */
final private function __clone() {}
final private function __clone(): void {}

/** @return bool */
public function inNamespace() {}
Expand Down Expand Up @@ -199,7 +199,7 @@ public function setAccessible(bool $accessible) {}

class ReflectionClass implements Reflector
{
final private function __clone() {}
final private function __clone(): void {}

public function __construct(object|string $objectOrClass) {}

Expand Down Expand Up @@ -370,7 +370,7 @@ public function __construct(object $object) {}
class ReflectionProperty implements Reflector
{
/** @implementation-alias ReflectionClass::__clone */
final private function __clone() {}
final private function __clone(): void {}

public function __construct(object|string $class, string $property) {}

Expand Down Expand Up @@ -435,7 +435,7 @@ public function getAttributes(?string $name = null, int $flags = 0): array {}
class ReflectionClassConstant implements Reflector
{
/** @implementation-alias ReflectionClass::__clone */
final private function __clone() {}
final private function __clone(): void {}

public function __construct(object|string $class, string $constant) {}

Expand Down Expand Up @@ -472,7 +472,7 @@ public function getAttributes(?string $name = null, int $flags = 0): array {}
class ReflectionParameter implements Reflector
{
/** @implementation-alias ReflectionClass::__clone */
final private function __clone() {}
final private function __clone(): void {}

/** @param string|array|object $function */
public function __construct($function, int|string $param) {}
Expand Down Expand Up @@ -551,7 +551,7 @@ public function getAttributes(?string $name = null, int $flags = 0): array {}
abstract class ReflectionType implements Stringable
{
/** @implementation-alias ReflectionClass::__clone */
final private function __clone() {}
final private function __clone(): void {}

/** @return bool */
public function allowsNull() {}
Expand All @@ -576,7 +576,7 @@ public function getTypes(): array {}
class ReflectionExtension implements Reflector
{
/** @implementation-alias ReflectionClass::__clone */
final private function __clone() {}
final private function __clone(): void {}

public function __construct(string $name) {}

Expand Down Expand Up @@ -619,7 +619,7 @@ public function isTemporary() {}
class ReflectionZendExtension implements Reflector
{
/** @implementation-alias ReflectionClass::__clone */
final private function __clone() {}
final private function __clone(): void {}

public function __construct(string $name) {}

Expand Down Expand Up @@ -648,7 +648,7 @@ public static function fromArrayElement(array $array, int|string $key): ?Reflect
public function getId(): string {}

/** @implementation-alias ReflectionClass::__clone */
private function __clone() {}
private function __clone(): void {}

private function __construct() {}
}
Expand All @@ -661,7 +661,7 @@ public function isRepeated(): bool {}
public function getArguments(): array {}
public function newInstance(): object {}

private function __clone() {}
private function __clone(): void {}

private function __construct() {}
}

0 comments on commit 47bbfe1

Please sign in to comment.