Skip to content

Haehnchen/idea-php-annotation-plugin

Repository files navigation

IntelliJ IDEA - PhpStorm PHP Annotations / Attributes

Build Status Version Downloads Downloads last month Donate to this project using Paypal

Provides PHP annotation extended Attribute support and for PhpStorm / IntelliJ IDEA and references for "Code > Optimize Imports" action.

Key Value
Plugin url https://plugins.jetbrains.com/plugin/7320
Id de.espend.idea.php.annotation
Changelog CHANGELOG

Install

  • Download plugin or install directly out of PhpStorm
  • Force file reindex if necessary with: File -> Invalidate Cache

Settings

PHP > Annotations / Attributes

Round brackets

/**
 * @Foo<caret>()
 * @Foo<caret>
 */
class NotBlank extends Constraint {}

Use / Import alias

PHP -> Annotations / Attributes -> Use Alias

Annotations
use Doctrine\ORM\Mapping as ORM;

/**
 * @Id() -> @ORM\Id()
 * @NotBlank() -> @Assert\NotBlank()
 */
class Foo {}
Attributes
use Doctrine\ORM\Mapping as ORM;

#[Id] -> #[ORM\Id()]
#[NotBlank] -> #[Assert\NotBlank()]
class Foo {}

Class LineMarker

LineMarker which provide navigation to annotation class usages

namespace Doctrine\ORM\Mapping;

/**
 * @Annotation
 */
final class Entity implements Annotation
{
}

Targeting

/**
 * @ORM\Entity()
 */

Annotation Class Detection

  • Every class with @Annotation inside class doc block is detected on file indexing
  • Annotation Properties on property names
  • Property value types
  • @ENUM Tags
/**
 * @Annotation
 */
class NotBlank extends Constraint {
    public $message = 'This value should not be blank.';
    public $groups = array();

    /**
     * @var bool|boolean
     */
    public $option = false;

    /**
     *
     * @Enum({"AUTO", "SEQUENCE", "TABLE", "IDENTITY", "NONE", "UUID", "CUSTOM"})
     */
    public $strategy = 'AUTO';

    /**
     * @var array<string>
     */
    public $cascade;
    
    /**
     * @var mixed|foobar|bool
     */
    public $mixed;
}

https://www.doctrine-project.org/projects/doctrine-annotations/en/latest/custom.html#attribute-types

/**
 * @Annotation
 *
 * @Attributes({
 *   @Attribute("stringProperty", type = "string"),
 *   @Attribute("annotProperty",  type = "bool"),
 * })
 */
 *
 * @Attributes(
 *   @Attribute("stringProperty", type = "string"),
 *   @Attribute("annotProperty",  type = "bool"),
 * )
 */
class Foobar {}

Annotation Target Detection

@Target is used to attach annotation, if non provided its added to "ALL list"

/**
 * @Annotation
 * @Target("PROPERTY", "METHOD", "CLASS", "ALL")
 */
class NotBlank extends Constraint {
    public $message = 'This value should not be blank.';
}

Extension Points

Plugins provides several extension points, which allows external plugins to provide additional. See some examples on Symfony2 Plugin

Example for extension points.

<extensionPoints>
      <extensionPoint name="PhpAnnotationCompletionProvider" interface="de.espend.idea.php.annotation.extension.PhpAnnotationCompletionProvider"/>
      <extensionPoint name="PhpAnnotationReferenceProvider" interface="de.espend.idea.php.annotation.extension.PhpAnnotationReferenceProvider"/>
      <extensionPoint name="PhpAnnotationDocTagGotoHandler" interface="de.espend.idea.php.annotation.extension.PhpAnnotationDocTagGotoHandler"/>
      <extensionPoint name="PhpAnnotationDocTagAnnotator" interface="de.espend.idea.php.annotation.extension.PhpAnnotationDocTagAnnotator"/>
      <extensionPoint name="PhpAnnotationGlobalNamespacesLoader" interface="de.espend.idea.php.annotation.extension.PhpAnnotationGlobalNamespacesLoader"/>
      <extensionPoint name="PhpAnnotationVirtualProperties" interface="de.espend.idea.php.annotation.extension.PhpAnnotationVirtualProperties"/>
      
      <!-- Custom class alias mapping: "ORM" => "Doctrine\\ORM\\Mapping" -->
      <extensionPoint name="PhpAnnotationUseAlias" interface="de.espend.idea.php.annotation.extension.PhpAnnotationUseAlias"/>
</extensionPoints>

Usage

<extensions defaultExtensionNs="de.espend.idea.php.annotation">
  <PhpAnnotationExtension implementation="de.espend.idea.php.annotation.completion.PhpAnnotationTypeCompletionProvider"/>
</extensions>

PHP Attributes Bridge

All extensions points providing support to DocBlock and PHP Attributes at the same time.

use Symfony\Component\Validator\Constraints\NotBlank;

#[NotBlank(message: 'An empty file is not allowed.')]
/* @NotBlank({message: "An empty file is not allowed."}) */
#[Template('foo.html.twig')]
/* @Template("foo.html.twig") */

Completion confidence

Annoying pressing completion shortcut? Plugin provides a nice completion confidence to open completion popover on several conditions

/**
 * @<caret>
 * <caret>
 */

Static values

    /**
     * @DI\Observe(SomethingEvents::PRE_UPDATE)
     */

Doctrine

ORM: Property generator

class Foo {
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     */
    public $id<caret>;
}
class Foo {
    #[ORM\Id]
    #[ORM\GeneratedValue(strategy: 'AUTO')]
    #[ORM\Column(type: 'integer')]
    public $id<caret>;
}

ORM: class entity generator

#[ORM\Entity(repositoryClass: \Foo::class)]
#[ORM\Table(name: 'bike')]
class Foo { }

ORM: repository class generator / intention

/**
 * @ORM\Entity(repositoryClass="UnknownClass")
 */
class Foo { }
/**
 * @ORM\Entity<caret>
 */
class Foo { }

ORM: repository class completion

/**
 * @ORM\Entity(repositoryClass="<caret>")
 */

PHP Toolbox

Provides integration for PHP Toolbox

Default and property values

use Symfony\Component\Routing\Annotation\Route;

/**
 * @Route("<caret>")
 * @Route(condition="<caret>")
 */
{
  "registrar":[
    {
      "signatures":[
        {
          "class": "Symfony\\Component\\Routing\\Annotation\\Route",
          "type": "annotation"
        },
        {
          "class": "Symfony\\Component\\Routing\\Annotation\\Route",
          "field": "condition",
          "type": "annotation"
        }
      ],
      "provider":"foo",
      "language":"php"
    }
  ],
}

Property array values

use Symfony\Component\Routing\Annotation\Route;

/**
 * @Route(methods={"<caret>"})
 */
{
  "registrar":[
    {
      "language":"php",
      "provider":"methods",
      "signatures":[
        {
          "class": "Symfony\\Component\\Routing\\Annotation\\Route",
          "type": "annotation_array",
          "field": "methods"
        }
      ]
    }
  ],
  "providers": [
    {
      "name": "methods",
      "items":[
        {
          "lookup_string": "POST"
        }
      ]
    }
  ]
}