Skip to content

Instantly share code, notes, and snippets.

@mindplay-dk
Last active October 4, 2018 02:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mindplay-dk/ebd5e4f7da51da3c4e56232adef41b46 to your computer and use it in GitHub Desktop.
Save mindplay-dk/ebd5e4f7da51da3c4e56232adef41b46 to your computer and use it in GitHub Desktop.
annotations
<?php
class Route
{
public $pattern;
public $method;
public function __construct($pattern, $method)
{
$this->pattern = $pattern;
$this->method = $method;
}
public static function get($pattern) {
return new self($pattern, "GET");
}
}
class Filter
{
public $name;
public function __construct($name)
{
$this->name = $name;
}
}
class UserController
{
<< Route::get('^/users/\w+$') >>
<< new Filter("membership") >>
public function showProfile()
{
// ...
}
}
$method = new ReflectionMethod(UserController::class, "showProfile");
var_dump($method->getAnnotations()); // => array(0 => {Route}, 1 => {Filter})
var_dump($method->getAnnotations(Filter::class)); // => array(0 => {Filter})
<?php
// C# style annotation rules can be implemented entirely in userland
class Usage
{
public $where;
public function __construct($where) {
$this->where = $where;
}
public static $classes = new Usage("classes");
public static $methods = new Usage("methods");
public static $properties = new Usage("properties");
public static $multiple = new Usage("multiple");
public static $inherit = new Usage("inherit");
}
class Annotations
{
public function getFromClass($class) {
// get class reflection
// collect annotations from class and recursively from parents
// for each annotation
// get Usage annotations from the annotation-class
// validate usage
// validate cardinality (single or multiple)
// apply inheritance logic (filter overridden annotations)
// return annotations
}
public function getFromMethod($class, $method) {
// ...
}
public function getFromProperty($class, $property) {
// ...
}
}
@brzuchal
Copy link

@fesor there is simple solution for this problem - abandon @ silent operator for error supression.
It's a huge BC break but IMHO using error supression provides more complexity and using exceptions instead would provide clean way for handling errors there is try/catch for that - that would be awesome change in my opinion.

@brzuchal
Copy link

The STFU operator @ should be considered as bad practice, IMHO those usages should be replaced with valid exception handling. Removing STFU could be marked as deprecated and then some users would reflect and refactor for right way of exception handling but also part of users would just blow deprecation errors by adding additional STFU which might cause unproper usage of $ret = @@foo();.
Maybe I'm just thinking loud and that's all.

@eman1986
Copy link

eman1986 commented Oct 4, 2018

I agree <<>> is ugly, I much prefer @ or @@

using @ to suppress errors is super bad in my opinion, I last used that in 2002 when I was young and stupid, I now handle that error instead of sweep it under the rug.

I'm honestly ok with busting BC as if someone is going to upgrade to a new version of PHP, they should expect legacy code to ge a little bruised up in the process and its more of knowing whats going to break before upgrading your system. especially if this makes the cut for PHP 8, I'd like to see a lot of things get disrupted to clean up the API finally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment