Skip to content

feat: Add support for Traits#21

Merged
gchtr merged 5 commits into
mainfrom
traitsupport
Apr 20, 2026
Merged

feat: Add support for Traits#21
gchtr merged 5 commits into
mainfrom
traitsupport

Conversation

@Levdbas

@Levdbas Levdbas commented Apr 20, 2026

Copy link
Copy Markdown
Member

Add Trait Documentation Support

Summary

This PR adds comprehensive support for documenting PHP traits in Teak, including standalone trait documentation and trait method/property inheritance in classes that use them.

Changes

Core Trait Support

1. Trait Documentation as Standalone Entities

  • Modified: lib/Console/ClassReferenceHandler.php

    • Added $traitMap to index all available traits
    • Added trait processing loop in generateClassList() to document traits alongside classes
    • Traits are now included in the generated class list
  • Modified: lib/Compiler/ClassLinkList.php

    • Updated generate() method to process traits for cross-linking in documentation
    • Traits can now be linked just like classes

2. Class Support for Trait Methods and Properties

  • Modified: lib/Reflection/ClassReflection.php

    • Added $traitMethods and $traitProperties arrays to track inherited trait members
    • Updated constructor to accept both Class_ and Trait_ objects
    • Added addTraitInformation() method to collect trait members
    • Updated getMethods() to include trait methods with proper override priority (class methods > trait methods > parent methods)
    • Updated getProperties() to include trait properties
    • Added getUsedTraits() method to expose which traits a class uses
    • Added method_exists() checks for trait compatibility in getParent() and getInterfaces()
  • Modified: lib/Reflection/Reflection.php

    • Updated type hints to include Trait_ alongside Class_, Method, and Property
  • Modified: lib/Console/ClassReferenceHandler.php

    • Added Trait_ import
    • Updated generateClassList() to detect and process trait usage via getUsedTraits()
    • Classes now automatically inherit methods and properties from their traits

3. Trait Usage Display

  • Created: lib/Compiler/Class_/TraitList.php

    • New compiler class to display which traits a class uses
    • Follows the same pattern as ParentList and InterfaceList
    • Displays "This class uses the trait TraitName" for single traits
    • Displays "This class uses the traits Trait1, Trait2" for multiple traits
  • Modified: lib/Compiler/Class_/ApiTable.php

    • Integrated TraitList alongside ParentList and InterfaceList
    • Trait usage now appears in the Overview section of class documentation

Testing Infrastructure

PHPUnit Setup

  • Modified: composer.json

    • Added phpunit/phpunit: ^10.0 as a dev dependency
  • Modified: phpunit.xml

    • Upgraded to PHPUnit 10 configuration format
    • Added proper XML namespace declarations
    • Configured cache directory and source coverage
  • Modified: .gitignore

    • Added .phpunit.cache directory

Test Coverage

  • Created: tests/testclasses/TestTrait.php

    • Sample trait with documented properties and methods for testing
  • Modified: tests/testclasses/TestClass.php

    • Updated to use TestTrait for integration testing
  • Modified: tests/ClassCompilerTest.php

    • Added testTraitDocumentation() - verifies standalone trait documentation
    • Added testClassWithTraitShowsTraitMethods() - verifies trait members appear in class documentation and trait usage statement is displayed

Documentation Output

When a class uses a trait, the generated documentation now includes:

## Overview

*This class uses the trait `Namespace\TraitName`*  

### Properties

| Name | Type | Description |
| --- | --- | --- |
| $trait_property | `string` | Trait property description. |
| $class_property | `string` | Class property description. |

### Methods

| Name | Return Type | Summary/Returns |
| --- | --- | --- |
| trait_method() | `string` | Trait method summary. |
| class_method() | `void` | Class method summary. |

Benefits

  1. Complete Documentation: Traits are now fully documented as first-class citizens
  2. Inheritance Visibility: Classes automatically show inherited trait methods and properties
  3. Clear Attribution: Documentation clearly states which traits a class uses
  4. Proper Linking: Traits can be cross-referenced in documentation
  5. Method Priority: Correctly handles method override priority (class > trait > parent)

Test Results

Class Compiler
 ✔ Trait documentation
 ✔ Class with trait shows trait methods

Tests: 3, Assertions: 9

All tests pass successfully.

Breaking Changes

None. This is a backward-compatible addition that extends existing functionality.

@gchtr gchtr changed the title Traitsupport feat: Add support for Traits Apr 20, 2026
@gchtr gchtr merged commit e524a4b into main Apr 20, 2026
2 checks passed
@gchtr

gchtr commented Apr 20, 2026

Copy link
Copy Markdown
Member

I tested it, this looks good!

This was referenced Apr 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants