xp-framework / compiler

Compiles future PHP to today's PHP.
19 stars 0 forks source link

Implement unchecked types #156

Closed thekid closed 11 months ago

thekid commented 1 year ago

These types only end up in the type meta information.

class Fixture {
  public function verified(string $param) { }
  public function unchecked(@string $param) { }
}

$f= new Fixture();
$t->verified(null);  // Exception
$t->unchecked(null); // Works

This is equivalent to using API docs only; however, the syntax is checked, whereas in the following, it isn't:

class Fixture {
  public function verified(string $param) { }

  /** @param string $param */
  public function unchecked($param) { }
}

In reflection, a parameter type is available but not reported as a constraint.

use lang\Reflection;

$r= Reflection::type(Fixture::class);

// Verified method
$c= $r->method('verified')->parameter(0)->constraint();
$c->type();    // Primitive::$STRING
$c->present(); // true

// Unchecked method
$c= $r->method('unchecked')->parameter(0)->constraint();
$c->type();    // Primitive::$STRING
$c->present(); // false