phpunit icon indicating copy to clipboard operation
phpunit copied to clipboard

Constraints cannot be implemented without using internal class ExpectationFailedException

Open nreynis opened this issue 6 months ago • 2 comments

It is currently impossible to implement a custom Constraint without triggering a static analysis error:

------ --------------------------------------------------------------------------------------------------------------------------------------
  Line   test/unit/Domain/Chat/MessageConstraint.php
 ------ --------------------------------------------------------------------------------------------------------------------------------------
  26     Call to method __construct() of internal class PHPUnit\Framework\ExpectationFailedException from outside its root namespace PHPUnit.
         🪪 method.internalClass
  26     Instantiation of internal class PHPUnit\Framework\ExpectationFailedException.
         🪪 new.internalClass
  39     Call to method __construct() of internal class PHPUnit\Framework\ExpectationFailedException from outside its root namespace PHPUnit.
         🪪 method.internalClass
  39     Instantiation of internal class PHPUnit\Framework\ExpectationFailedException.
         🪪 new.internalClass
 ------ --------------------------------------------------------------------------------------------------------------------------------------

Expected behavior

ExpectationFailedException should not be marked as internal, or Constraint should allow throwing other kind of exceptions.

nreynis avatar Jun 19 '25 12:06 nreynis

Does it suffice to remove @internal from ExpectationFailedException or must its parent classes not not be internal?

sebastianbergmann avatar Jun 19 '25 12:06 sebastianbergmann

I tested and removing @internal from ExpectationFailedException is enough to make PHPStan happy.

nreynis avatar Jun 19 '25 13:06 nreynis