MethodDecorator.php 2.36 KiB
<?php declare(strict_types=1);
/*
 * Copyright (C) 2016-2017 IRSTEA
 * All rights reserved.
 */
namespace Irstea\PlantUmlBundle\Model\Decorator;
use Irstea\PlantUmlBundle\Model\ClassVisitorInterface;
use Irstea\PlantUmlBundle\Model\DecoratorInterface;
use Irstea\PlantUmlBundle\Model\Node\Member\Member;
use Irstea\PlantUmlBundle\Model\Node\Member\MemberInterface;
use Irstea\PlantUmlBundle\Model\NodeInterface;
use ReflectionClass;
use ReflectionMethod;
/**
 * Description of AttributeDecorator.
class MethodDecorator implements DecoratorInterface
    public function decorate(ReflectionClass $class, NodeInterface $node, ClassVisitorInterface $visitor)
        foreach ($class->getMethods() as $method) {
            /* @var $method ReflectionMethod */
            if ($method->getDeclaringClass() != $class || $this->isAccessor($method, $class)) {
                continue;
            $node->addMethod(
                new Member(
                    $method->getName() . '(...)',
                    '',
                    $method->isPrivate() ? MemberInterface::PRIVATE_ :
                        ($method->isProtected() ? MemberInterface::PROTECTED_ :
                            ($method->isPublic() ? MemberInterface::PUBLIC_ :
                                MemberInterface::UNKNOWN))
    /**
     * @param ReflectionMethod $method
     * @param ReflectionClass  $class
     * @return bool
    protected function isAccessor(ReflectionMethod $method, ReflectionClass $class)
        if (!$method->isPublic() || $method->isAbstract() || $method->getDeclaringClass()->isInterface()) {
            return false;
        if ($method->getNumberOfParameters() === 0 && preg_match('/(?:get|is)(\w+)/', $method->getName(), $groups)) {
            $name = lcfirst($groups[1]);
        } elseif ($method->getNumberOfParameters() === 1 && preg_match('/(?:set|add|remove)(\w+)/', $method->getName(), $groups)) {
            $name = lcfirst($groups[1]);
        } else {
            return false;
        if (!$class->hasProperty($name)) {
            return false;
        $property = $class->getProperty($name);
        return $property->isStatic() == $method->isStatic();
    public function toConfig(array &$conf)
7172737475
{ $conf['decorators'][] = 'methods'; } }