Skip to content

Commit

Permalink
Merge pull request #804 from DerManoMann/traits-inheriting-traits
Browse files Browse the repository at this point in the history
Allow traits also to inherit traits
  • Loading branch information
DerManoMann committed Jul 5, 2020
2 parents dc393e7 + 6781d4f commit b0719f1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
15 changes: 10 additions & 5 deletions Examples/using-traits/using-traits-inherit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@ components:
schemas:
BellsAndWhistles:
title: 'Bells and Whistles trait'
properties:
plating:
description: 'The plating.'
example: gold
type: object
allOf:
-
$ref: '#/components/schemas/Bells'
-
$ref: '#/components/schemas/Whistles'
-
properties:
plating:
description: 'The plating.'
example: gold
Blink:
title: 'Blink trait'
properties:
Expand Down
16 changes: 8 additions & 8 deletions src/Analysis.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,22 +275,22 @@ public function getInterfacesOfClass($class, $direct = false)
}

/**
* Get the list of traits used by the given class or by classes which it extends.
* Get the list of traits used by the given class/trait or by classes which it extends.
*
* @param string $class The class name.
* @param string $source The source name.
* @param bool $direct Flag to find only the actual class traits.
* @return array Map of class => definition pairs of traits.
*/
public function getTraitsOfClass($class, $direct = false)
public function getTraitsOfClass($source, $direct = false)
{
$classes = $direct ? [] : array_keys($this->getSuperClasses($class));
$sources = $direct ? [] : array_keys($this->getSuperClasses($source));
// add self
$classes[] = $class;
$sources[] = $source;

$definitions = [];
foreach ($classes as $clazz) {
if (isset($this->classes[$clazz])) {
$definition = $this->classes[$clazz];
foreach ($sources as $sourze) {
if (isset($this->classes[$sourze]) || isset($this->traits[$sourze])) {
$definition = isset($this->classes[$sourze]) ? $this->classes[$sourze] : $this->traits[$sourze];
if (isset($definition['traits'])) {
foreach ($definition['traits'] as $trait) {
if (array_key_exists($trait, $this->traits)) {
Expand Down
5 changes: 3 additions & 2 deletions src/Processors/InheritTraits.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ public function __invoke(Analysis $analysis)
{
$schemas = $analysis->getAnnotationsOfType(Schema::class);
foreach ($schemas as $schema) {
if ($schema->_context->is('class')) {
if ($traits = $analysis->getTraitsOfClass($schema->_context->fullyQualifiedName($schema->_context->class), true)) {
if ($schema->_context->is('class') || $schema->_context->is('trait')) {
$source = $schema->_context->class ?: $schema->_context->trait;
if ($traits = $analysis->getTraitsOfClass($schema->_context->fullyQualifiedName($source), true)) {
if ($schema->allOf === UNDEFINED) {
$schema->allOf = [];
}
Expand Down

0 comments on commit b0719f1

Please sign in to comment.