Skip to content

Repository files navigation

Memio's Models

Describe PHP code (classes/interfaces with their constants, properties, methods, method arguments and even PHPdoc) by constructing "Model" objects.

Note: This package is part of Memio, a highly opinionated PHP code generator. Have a look at the main repository.

Installation

Install it using Composer:

composer require memio/model:^3.0

Example

Let's say we want to describe the following method:

/** * @api */publicfunction doSomething(ValueObject$valueObject, int$type = self::TYPE_ONE, bool$option = true);

In order to do so, we'd need to write the following:

<?phprequire__DIR__.'/vendor/autoload.php';
useMemio\Model\Argument;
useMemio\Model\Method;
useMemio\Model\Phpdoc\ApiTag;
useMemio\Model\Phpdoc\MethodPhpdoc;
useMemio\Model\Phpdoc\ParameterTag;
$method = (newMethod('doSomething'))
->setPhpdoc((newMethodPhpdoc())
->addParameterTag(newParameterTag('Vendor\Project\ValueObject', 'valueObject'))
->addParameterTag(newParameterTag('int', 'type'))
->addParameterTag(newParameterTag('bool', 'option'))
->addApiTag(newApiTag())
)
->addArgument(newArgument('Vendor\Project\ValueObject', 'valueObject'))
->addArgument((newArgument('int', 'type'))
->setDefaultValue('self::TYPE_ONE')
)
->addArgument((newArgument('bool', 'option'))
->setDefaultValue('true')
)
;

Usually models aren't described manually like this, they would be built dynamically:

// Let's say we've received the following two parameters:$methodName = 'doSomething';
$arguments = [new \Vendor\Project\ValueObject(), ValueObject::TYPE_ONE, true];
$method = newMethod($methodName);
$phpdoc = (newMethodPhpdoc())->setApiTag(newApiTag());
$index = 1;
foreach ($argumentsas$rawArgument) {
$type = is_object($rawArgument) ? get_class($argument) : gettype($rawArgument);
$name = 'argument'.$index++;
$argument = newArgument($type, $name);
$method->addArgument($argument);
$phpdoc->addParameterTag(newParameterTag($type, $name));
}
$method->setPhpdoc($phpdoc);

We can build dynamically the models using a configuration file, user input, existing source code... Possibilities are endless!

Once built these models can be further tweaked, and converted to another format: an array, source code, etc... Again, the possibilities are endless!

Have a look at the main respository to discover the full power of Medio.

Want to know more?

Memio uses phpspec, which means the tests also provide the documentation. Not convinced? Then clone this repository and run the following commands:

make lib-init # Set up Docker environmentmake phpspec arg='--format pretty' # Run the specifications

Note: Run make or make help to see all available commands.

You can see the current and past versions using one of the following:

And finally some meta documentation:

Roadmap

  • get rid of Type
  • extract Import (use statement) from FullyQualifiedName
  • get rid of FullyQualifiedName
  • support more PHPdoc stuff
  • support annotations

About

[sub] Describe PHP code by building Memio objects

Resources

Contributing

Stars

5 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages