Skip to content

PHPDoc tags and annotations

USAMI Kenta edited this page Oct 6, 2017 · 4 revisions

"tag" vs "annotation"

@exampleのような@を伴った記法は「タグ」または「アノテーション」と呼ばれます。これらは似たものですが、違った特徴があります。

DocComment

DocCommentはクラス・関数・メソッド・プロパティなどに記述することができる特殊なコメントです。通常のブロックコメントは /* … */ ですが、DocCommentは必ず /** … */ です。

<?php

// This is NOT DocComment
class A {}

/*
 * This is NOT DocComment
 */
class B {}

/**
 * This is DocComment!!!!
 */
class C {}


$a = new ReflectionClass(A::class);
var_dump($a->getDocComment());
// => bool(false)

$b = new ReflectionClass(B::class);
var_dump($b->getDocComment());
// => bool(false)

$c = new ReflectionClass(C::class);
var_dump($c->getDocComment());
// => string(33) "/**
//  * This is DocComment!!!!
//  */"

annotation

/**
 * @Annotation
 * @Target("CLASS")
 */

アノテーションは主にDoctrineによって導入されました。それは括弧()を伴った複雑な式を含むことがあります。アノテーションは典型的には大文字アルファベットとバックスラッシュ\から構成されるので、タグとは少し違った特徴があります。

Doctrineによる定義はDoctrine Annotationsにあります。パーサーの実装はdoctrine/annotations: Annotations Docblock Parserです。

tag

/**
 * @deprecated
 * @license MIT
 */

タグの文法はアノテーションに比べて比較的単純です。トークンは基本的にスペース で分割されるので、構文解析は基本的に容易です。 (ただし、それぞれのタグに固有の癖に対処しなければいけません)

List of implemetations

phpDocumentor

これらのタグはList of tagsTag referenceで定義されます。

  • @api
  • @author
  • @category
  • @copyright
  • @deprecated
  • @example
  • @filesource
  • @global
  • @ignore
  • @internal
  • @license
  • @link
  • @method
  • @package
  • @param
  • @property
  • @property-read
  • @property-write
  • @return
  • @see
  • @since
  • @source
  • @subpackage
  • @throws
  • @todo
  • @uses and @used-by
  • @var
  • @version

Phan

これらのタグはAnnotating Your Source Code · phan/phan Wikiで定義されます。

  • @var
  • @param
  • @return
  • @method
  • @deprecated
  • @internal
  • @suppress
  • @property
  • @override
  • @phan-closure-scope

PhpStorm

独自実装されたこれらのタグは統一された文書がないので、完全なリストはありません。

PHPUnit

これらのタグはPHPUnit Manual – Appendix B. Annotationsで定義されます。

  • @author
  • @after
  • @afterClass
  • @backupGlobals
  • @backupStaticAttributes
  • @before
  • @beforeClass
  • @codeCoverageIgnore*
  • @covers
  • @coversDefaultClass
  • @coversNothing
  • @dataProvider
  • @depends
  • @expectedException
  • @expectedExceptionCode
  • @expectedExceptionMessage
  • @expectedExceptionMessageRegExp
  • @group
  • @large
  • @medium
  • @preserveGlobalState
  • @requires
  • @runTestsInSeparateProcesses
  • @runInSeparateProcess
  • @small
  • @test
  • @testdox
  • @ticket
  • @uses

PHP CodeSniffer (phpcs)

phpcsの検査から除外されるためのコメント表記です。ただし、DocCommentではなく、通常のコメントに記述します。これらの表記はAdvanced Usage · squizlabs/PHP_CodeSniffer Wikiに記載されます。

  • @codingStandardsIgnoreFile
  • @codingStandardsIgnoreStart and @codingStandardsIgnoreEnd
  • @codingStandardsIgnoreLine