Skip to content

Commit 47bbfe1

Browse files
committed
Require stubs to declare return types for magic methods when possible
Closes GH-6376
1 parent 0fb2374 commit 47bbfe1

9 files changed

+177
-163
lines changed

Zend/zend_exceptions.stub.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ public function getTraceAsString(): string;
2222

2323
class Exception implements Throwable
2424
{
25-
final private function __clone() {}
25+
final private function __clone(): void {}
2626

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

29+
/** @return void */
2930
public function __wakeup() {}
3031

3132
final public function getMessage(): string {}
@@ -56,12 +57,15 @@ final public function getSeverity(): int {}
5657
class Error implements Throwable
5758
{
5859
/** @implementation-alias Exception::__clone */
59-
final private function __clone() {}
60+
final private function __clone(): void {}
6061

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

64-
/** @implementation-alias Exception::__wakeup */
65+
/**
66+
* @return void
67+
* @implementation-alias Exception::__wakeup
68+
*/
6569
public function __wakeup() {}
6670

6771
/** @implementation-alias Exception::getMessage */

Zend/zend_exceptions_arginfo.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: bc49b326136997660887b12f0c59f8a57b17ecaf */
2+
* Stub hash: 3699b51b31e509c11435845c7e0d35a2608dd268 */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Throwable_getMessage, 0, 0, IS_STRING, 0)
55
ZEND_END_ARG_INFO()
@@ -20,7 +20,8 @@ ZEND_END_ARG_INFO()
2020

2121
#define arginfo_class_Throwable_getTraceAsString arginfo_class_Throwable_getMessage
2222

23-
#define arginfo_class_Exception___clone arginfo_class_Throwable_getCode
23+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Exception___clone, 0, 0, IS_VOID, 0)
24+
ZEND_END_ARG_INFO()
2425

2526
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Exception___construct, 0, 0, 0)
2627
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, message, IS_STRING, 0, "\"\"")
@@ -57,7 +58,7 @@ ZEND_END_ARG_INFO()
5758

5859
#define arginfo_class_ErrorException_getSeverity arginfo_class_Throwable_getLine
5960

60-
#define arginfo_class_Error___clone arginfo_class_Throwable_getCode
61+
#define arginfo_class_Error___clone arginfo_class_Exception___clone
6162

6263
#define arginfo_class_Error___construct arginfo_class_Exception___construct
6364

build/gen_stub.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -359,9 +359,9 @@ interface FunctionOrMethodName {
359359
public function getDeclaration(): string;
360360
public function getArgInfoName(): string;
361361
public function __toString(): string;
362-
public function isMagicMethod(): bool;
363362
public function isMethod(): bool;
364363
public function isConstructor(): bool;
364+
public function isDestructor(): bool;
365365
}
366366

367367
class FunctionName implements FunctionOrMethodName {
@@ -403,15 +403,15 @@ public function __toString(): string {
403403
return $this->name->toString();
404404
}
405405

406-
public function isMagicMethod(): bool {
406+
public function isMethod(): bool {
407407
return false;
408408
}
409409

410-
public function isMethod(): bool {
410+
public function isConstructor(): bool {
411411
return false;
412412
}
413413

414-
public function isConstructor(): bool {
414+
public function isDestructor(): bool {
415415
return false;
416416
}
417417
}
@@ -443,17 +443,17 @@ public function __toString(): string {
443443
return "$this->className::$this->methodName";
444444
}
445445

446-
public function isMagicMethod(): bool {
447-
return strpos($this->methodName, '__') === 0;
448-
}
449-
450446
public function isMethod(): bool {
451447
return true;
452448
}
453449

454450
public function isConstructor(): bool {
455451
return $this->methodName === "__construct";
456452
}
453+
454+
public function isDestructor(): bool {
455+
return $this->methodName === "__destruct";
456+
}
457457
}
458458

459459
class ReturnInfo {
@@ -879,7 +879,7 @@ function parseFunctionLike(
879879
}
880880

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

ext/date/php_date.stub.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,15 @@ public function getTimestamp();
134134
/** @return DateInterval|false */
135135
public function diff(DateTimeInterface $targetObject, bool $absolute = false);
136136

137+
/** @return void */
137138
public function __wakeup();
138139
}
139140

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

145+
/** @return void */
144146
public function __wakeup() {}
145147

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

251+
/** @return void */
249252
public function __wakeup() {}
250253

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

369+
/** @return void */
366370
public function __wakeup() {}
367371

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

392+
/** @return void */
388393
public function __wakeup() {}
389394

390395
/** @return DateInterval */
@@ -413,6 +418,7 @@ public function getDateInterval() {}
413418
/** @return int|null */
414419
public function getRecurrences() {}
415420

421+
/** @return void */
416422
public function __wakeup() {}
417423

418424
/** @return DatePeriod */

ext/date/php_date_arginfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 04954b7aac5b3ee8e789b3ddd254ad5eda299c2b */
2+
* Stub hash: 07d0e6179d838847d4a5d454f8b56438f8df19c0 */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_strtotime, 0, 1, MAY_BE_LONG|MAY_BE_FALSE)
55
ZEND_ARG_TYPE_INFO(0, datetime, IS_STRING, 0)

ext/reflection/php_reflection.stub.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ interface Reflector extends Stringable
1919
abstract class ReflectionFunctionAbstract implements Reflector
2020
{
2121
/** @implementation-alias ReflectionClass::__clone */
22-
final private function __clone() {}
22+
final private function __clone(): void {}
2323

2424
/** @return bool */
2525
public function inNamespace() {}
@@ -199,7 +199,7 @@ public function setAccessible(bool $accessible) {}
199199

200200
class ReflectionClass implements Reflector
201201
{
202-
final private function __clone() {}
202+
final private function __clone(): void {}
203203

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

@@ -370,7 +370,7 @@ public function __construct(object $object) {}
370370
class ReflectionProperty implements Reflector
371371
{
372372
/** @implementation-alias ReflectionClass::__clone */
373-
final private function __clone() {}
373+
final private function __clone(): void {}
374374

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

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

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

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

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

556556
/** @return bool */
557557
public function allowsNull() {}
@@ -576,7 +576,7 @@ public function getTypes(): array {}
576576
class ReflectionExtension implements Reflector
577577
{
578578
/** @implementation-alias ReflectionClass::__clone */
579-
final private function __clone() {}
579+
final private function __clone(): void {}
580580

581581
public function __construct(string $name) {}
582582

@@ -619,7 +619,7 @@ public function isTemporary() {}
619619
class ReflectionZendExtension implements Reflector
620620
{
621621
/** @implementation-alias ReflectionClass::__clone */
622-
final private function __clone() {}
622+
final private function __clone(): void {}
623623

624624
public function __construct(string $name) {}
625625

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

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

653653
private function __construct() {}
654654
}
@@ -661,7 +661,7 @@ public function isRepeated(): bool {}
661661
public function getArguments(): array {}
662662
public function newInstance(): object {}
663663

664-
private function __clone() {}
664+
private function __clone(): void {}
665665

666666
private function __construct() {}
667667
}

0 commit comments

Comments
 (0)