Skip to content
This repository was archived by the owner on Jun 24, 2025. It is now read-only.
This repository was archived by the owner on Jun 24, 2025. It is now read-only.

Fatal error: Trait method "x" was already imported. #71

@POPSuL

Description

@POPSuL

Example:

TraitA.php

<?php

namespace T;

trait TraitA
{

    abstract public function x();
}

TraitB.php

<?php

namespace T;

trait TraitB
{

    abstract public function x();
}

TraitAggregate.php

<?php

namespace T;

trait TraitAggregate
{

    use TraitA, TraitB;
}

T.php

<?php

namespace T;

class T
{
    use TraitAggregate;

    public function x() {
        echo "foo";
    }
}

And test.php:

<?php

use TokenReflection\Broker;

require 'vendor/autoload.php';

$t = new \T\T();
$t->x();

$broker = new Broker(new Broker\Backend\Memory());
$broker->processDirectory('./src');

$class = $broker->getClass('T\T');
var_dump($class->getMethods());

Result:

 % php ./test.php
fooPHP Fatal error:  Uncaught exception 'TokenReflection\Exception\RuntimeException' with message 'Trait method "x" was already imported.' in /home/popsul/projects/traits/vendor/andrewsville/php-token-reflection/TokenReflection/ReflectionClass.php on line 734
Thrown when working with "T\TraitAggregate".

Stack trace:
#0 /home/popsul/projects/traits/vendor/andrewsville/php-token-reflection/TokenReflection/ReflectionClass.php(607): TokenReflection\ReflectionClass->getTraitMethods()
#1 /home/popsul/projects/traits/vendor/andrewsville/php-token-reflection/TokenReflection/ReflectionClass.php(701): TokenReflection\ReflectionClass->getMethods(NULL)
#2 /home/popsul/projects/traits/vendor/andrewsville/php-token-reflection/TokenReflection/ReflectionClass.php(607): TokenReflection\ReflectionClass->getTraitMethods()
#3 /home/popsul/projects/traits/test.php(14): TokenReflection\ReflectionClass->getMethods()
#4 {main}

  thrown in /home/popsul/projects/traits/vendor/andrewsville/php-token-reflection/TokenReflection/ReflectionClass.php on line 734

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions