2017 © Pedro Peláez
 

library php-property-info

Retrieve type and description of PHP properties using various sources

image

dunglas/php-property-info

Retrieve type and description of PHP properties using various sources

  • Tuesday, December 29, 2015
  • by dunglas
  • Repository
  • 0 Watchers
  • 27 Stars
  • 159,979 Installations
  • PHP
  • 4 Dependents
  • 0 Suggesters
  • 3 Forks
  • 1 Open issues
  • 7 Versions
  • 3 % Grown

The README.md

PHP Property Info

Deprecated: This library has been merged into the Symfony Framework. Please use and contribute to the Symfony PropertyInfo Component., (*1)

PHP doesn't support explicit type definition. This is annoying, especially when doing meta programming. Various libraries including but not limited to Doctrine ORM and the Symfony Validator provide their own type managing system. This library extracts various information including the type and documentation from PHP class property from metadata of popular sources:, (*2)

  • Setter method with type hint
  • PHPDoc DocBlock
  • Doctrine ORM mapping (annotation, XML, YML or custom format)
  • PHP 7 scalar typehint and return type
  • Hack (hacklang) types

Build Status SensioLabsInsight, (*3)

PHP Property info is part of the API Platform framework., (*4)

Installation

Use Composer to install the library:, (*5)

composer require dunglas/php-property-info

To use the PHPDoc extractor, install the phpDocumentator's Reflection library. To use the Doctrine extractor, install the Doctrine ORM., (*6)

Usage

<?php

// Use Composer autoload
require 'vendor/autoload.php';

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Id;

// PropoertyInfo uses
use PropertyInfo\Extractors\DoctrineExtractor;
use PropertyInfo\Extractors\PhpDocExtractor;
use PropertyInfo\Extractors\SetterExtractor;
use PropertyInfo\PropertyInfo;

/**
 * @Entity
 */
class MyTestClass
{
    /**
     * @Id
     * @Column(type="integer")
     */
    public $id;
    /**
     * This is a date (short description).
     *
     * With a long description.
     *
     * @var \DateTime
     */
    public $foo;
    private $bar;

    public function setBar(\SplFileInfo $bar)
    {
        $this->bar = $bar;
    }
}

// Doctrine initialization (necessary only to use the Doctrine Extractor)
$config = Setup::createAnnotationMetadataConfiguration([__DIR__], true);
$entityManager = EntityManager::create([
    'driver' => 'pdo_sqlite',
    // ...
], $config);

$doctrineExtractor = new DoctrineExtractor($entityManager->getMetadataFactory());
$phpDocExtractor = new PhpDocExtractor();
$setterExtractor = new SetterExtractor();

$propertyInfo = new PropertyInfo([$doctrineExtractor, $setterExtractor, $phpDocExtractor], [$phpDocExtractor]);

$fooProperty = new \ReflectionProperty('MyTestClass', 'foo');
var_dump($propertyInfo->getShortDescription($fooProperty));
var_dump($propertyInfo->getLongDescription($fooProperty));
var_dump($propertyInfo->getTypes($fooProperty));
var_dump($propertyInfo->getTypes(new \ReflectionProperty('MyTestClass', 'id')));
var_dump($propertyInfo->getTypes(new \ReflectionProperty('MyTestClass', 'bar')));

Output:, (*7)

string(35) "This is a date (short description)."
string(24) "With a long description."
array(1) {
  [0] =>
  class PropertyInfo\Type#162 (4) {
    public $type =>
    string(6) "object"
    public $class =>
    string(8) "DateTime"
    public $collection =>
    bool(false)
    public $collectionType =>
    NULL
  }
}
array(1) {
  [0] =>
  class PropertyInfo\Type#172 (4) {
    public $type =>
    string(3) "int"
    public $class =>
    NULL
    public $collection =>
    bool(false)
    public $collectionType =>
    NULL
  }
}
array(1) {
  [0] =>
  class PropertyInfo\Type#165 (4) {
    public $type =>
    string(6) "object"
    public $class =>
    string(11) "SplFileInfo"
    public $collection =>
    bool(false)
    public $collectionType =>
    NULL
  }
}

Try it yourself using Melody:, (*8)

php melody.phar run https://gist.github.com/dunglas/0a4982e4635c9514aede

TODO

  • [ ] Symfony Validator Component support

Credits

This library has been created by Kévin Dunglas., (*9)

The Versions

29/12 2015

0.2.x-dev

0.2.9999999.9999999-dev http://dunglas.fr

Retrieve type and description of PHP properties using various sources

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

validator phpdoc doctrine symfony type property

29/12 2015

v0.2.3

0.2.3.0 http://dunglas.fr

Retrieve type and description of PHP properties using various sources

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

validator phpdoc doctrine symfony type property

28/12 2015

dev-master

9999999-dev https://dunglas.fr

Retrieve type and description of PHP properties using various sources

  Sources   Download

MIT

The Requires

  • php >=5.3

 

The Development Requires

validator phpdoc doctrine symfony type property

28/12 2015

v0.2.2

0.2.2.0 https://dunglas.fr

Retrieve type and description of PHP properties using various sources

  Sources   Download

MIT

The Requires

  • php >=5.3

 

The Development Requires

validator phpdoc doctrine symfony type property

03/04 2015

v0.2.1

0.2.1.0 http://dunglas.fr

Retrieve type and description of PHP properties using various sources

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

validator phpdoc doctrine symfony type property

25/02 2015

v0.2.0

0.2.0.0 http://dunglas.fr

Retrieve type and description of PHP properties using various sources

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

validator phpdoc doctrine symfony type property

16/02 2015

v0.1.0

0.1.0.0 http://dunglas.fr

Retrieve type and description of PHP properties using various sources

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

validator phpdoc doctrine symfony type property