php-deal / framework

Design by Contract framework for PHP
MIT License
252 stars 22 forks source link

Violating contracts should throw ContractViolation #30

Closed icanhazstring closed 5 years ago

icanhazstring commented 5 years ago

Source branch: 1.x

A violation with a contract should not throw a DomainException with

Fatal error: Uncaught DomainException: Invalid return value received from the assertion body, only boolean or void can be returned in deal/demo/Account.php on line 51

To reproduce this, simply change the demo Account::getBalance to return any value instead 100.


It should instead raise an ContractViolation explaining what contract was violated with what value. Somthing like this:

PHP Fatal error:  Uncaught PhpDeal\Exception\ContractViolation: Contract $amount>0 && is_numeric($amount) violated with -1 for Demo\Account->deposit in XXX

Since this will change exceptions raised, this is assumed a breaking change. To change this, one has to change the AbstractContractAspect inside ensureContracts. If the $invocationResult equals false, a ContractViolation should be raised.

icanhazstring commented 5 years ago

Solved with 20f0cef0d012dbff8fdf5b67306a6ead68160636