Skip to content

Instantly share code, notes, and snippets.

@holyshared holyshared/.hhconfig
Last active Dec 23, 2018

Embed
What would you like to do?
user_attributes=

Typed User Attributes

  • ClassAttribute - クラス
  • EnumAttribute - 列挙型
  • TypeAliasAttribute - 型の別名
  • FunctionAttribute - 関数
  • MethodAttribute - メソッド
  • InstancePropertyAttribute - インスタンスプロパティ
  • StaticPropertyAttribute - 静的プロパティ
  • ParameterAttribute - 引数
<?hh //strict
namespace TypedUserAttributes;
final class Comment implements \HH\ParameterAttribute {
public function __construct(
private string $description
) {
}
public function __toString() : string {
return (string) $this->description;
}
}
<?hh //strict
namespace TypedUserAttributes;
use type \TypedUserAttributes\Version;
use type \TypedUserAttributes\Comment;
<<Version(2.1)>>
function createPost(
<<Comment("title of post")>> string $title,
<<Comment("content of post")>> string $content
): Post {
return new Post($title, $content);
}
<?hh //strict
namespace TypedUserAttributes;
require_once 'Post.hh';
require_once 'PostStatus.hh';
require_once 'Version.hh';
require_once 'Comment.hh';
require_once 'helper.hh';
use \ReflectionClass;
use \ReflectionFunction;
<<__EntryPoint>>
function main(): noreturn {
$class = new ReflectionClass(\TypedUserAttributes\Post::class);
$classVersion = $class->getAttributeClass(\TypedUserAttributes\Version::class);
\printf("class version: %s\n", $classVersion);
$methodVersion = $class->getMethod('getTitle')->getAttributeClass(\TypedUserAttributes\Version::class);
\printf("method version: %s\n", $methodVersion);
$func = new ReflectionFunction('\\TypedUserAttributes\\createPost');
$funcVersion = $func->getAttributeClass(\TypedUserAttributes\Version::class);
\printf("function version: %s\n", $funcVersion);
$params = $func->getParameters();
$titleDoc = $params[0]->getAttributeClass(\TypedUserAttributes\Comment::class);;
\printf("title doc: %s\n", $titleDoc);
$contentDoc = $params[1]->getAttributeClass(\TypedUserAttributes\Comment::class);;
\printf("content doc: %s\n", $contentDoc);
exit();
}
<?hh //strict
namespace TypedUserAttributes;
use type \TypedUserAttributes\Version;
<<Version(1.1)>>
final class Post {
public function __construct(
private string $title,
private string $content
) {
}
<<Version(1.2)>>
public function getTitle(): string {
return $this->title;
}
}
<?hh //strict
namespace TypedUserAttributes;
use type \TypedUserAttributes\Version;
<<Version(1.1)>>
enum PostStatus: string {
Published = 'published';
Reserved = 'reserved';
}
<?hh //strict
namespace TypedUserAttributes;
final class Version implements \HH\EnumAttribute, \HH\ClassAttribute, \HH\MethodAttribute, \HH\FunctionAttribute {
public function __construct(
private float $version
) {
}
public function __toString() : string {
return (string) $this->version;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.