fix(preprocessor): reject abstract method bodies and abstract private class methods (#62) --skip-tests
* fix(preprocessor): reject abstract method bodies and abstract private class methods Two abstract-method rules Zend enforces at compile time were missing: - an abstract method with a body was accepted and the body silently dropped; Zend fatals with "Abstract function A::f() cannot contain body" (applies to classes and traits alike, probed on 8.4.13) - `abstract private function` in a class can never be implemented, since private methods do not participate in overriding; Zend fatals with "Abstract function A::f() cannot be declared private". Traits keep accepting it (allowed since PHP 8.0: the consuming class supplies the private implementation) Zend reports the private-modifier error before the body error when both apply; the checks are ordered to match. * test(preprocessor): cover abstract method declaration rulesmaster
parent
f03c0b12c6
commit
58c3bb64b6
5 changed files with 61 additions and 0 deletions
@ -0,0 +1,4 @@ |
|||||||
|
<?php |
||||||
|
abstract class Job { abstract public function run(): void {} } |
||||||
|
|
||||||
|
function main() {} |
||||||
@ -0,0 +1,4 @@ |
|||||||
|
<?php |
||||||
|
abstract class Job { abstract private function run(): void; } |
||||||
|
|
||||||
|
function main() {} |
||||||
@ -0,0 +1,19 @@ |
|||||||
|
<?php |
||||||
|
trait JobTrait |
||||||
|
{ |
||||||
|
abstract private function run(): void; |
||||||
|
|
||||||
|
public function go(): void |
||||||
|
{ |
||||||
|
$this->run(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
class Job |
||||||
|
{ |
||||||
|
use JobTrait; |
||||||
|
|
||||||
|
private function run(): void {} |
||||||
|
} |
||||||
|
|
||||||
|
function main() {} |
||||||
@ -0,0 +1,23 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
/** |
||||||
|
* Zend abstract-method declaration rules: an abstract method may not |
||||||
|
* carry a body, and abstract private is only legal inside traits. |
||||||
|
*/ |
||||||
|
class AbstractMethodDeclarationTest extends BaseTest |
||||||
|
{ |
||||||
|
public function testAbstractMethodCannotContainBody(): void |
||||||
|
{ |
||||||
|
$this->exec('Abstract function `Job::run()` cannot contain body', 'abstract_rule_body.php'); |
||||||
|
} |
||||||
|
|
||||||
|
public function testAbstractClassMethodCannotBePrivate(): void |
||||||
|
{ |
||||||
|
$this->exec('Abstract function `Job::run()` cannot be declared private', 'abstract_rule_private.php'); |
||||||
|
} |
||||||
|
|
||||||
|
public function testAbstractPrivateTraitMethodIsAllowed(): void |
||||||
|
{ |
||||||
|
$this->compile('abstract_rule_private_trait_valid.php'); |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue