Page MenuHomeDevCentral

Complete CodeProperty implementation with unit tests
Open, NormalPublic

Description

The CodeProperty class has been drafted in D3860 but needs unit tests and validation of its behavior, particularly around nullable types.

good-first-issue This is a great opportunity to practice Test-Driven Development (TDD) and learn about PHP's reflection API.

Description of CodeProperty

The CodeProperty class provides reflection capabilities for class properties, similar to the existing CodeVariable class. The draft implementation includes methods to:

  • Check if a property exists
  • Determine property types (single, union, intersection)
  • Check if a property is nullable (via type hints or PHPDoc)

However, the implementation lacks comprehensive unit tests and some edge cases need validation.

It's assumed nullable types validation will require to update/tweak the implementation.

Tasks

1. Write Unit Tests (TDD Approach)

Create omnitools/tests/Reflection/CodePropertyTest.php with test coverage for:

Basic functionality:

  • testExists() - verify property existence checks work
  • testGetPropertyName() - verify property name retrieval
  • testGetClass() - verify class retrieval

Type detection:

  • testHasTypeForSimpleTypes() - test with int, string, bool, float, array
  • testHasTypeForObject() - test with class type hints
  • testHasTypeForMixed() - test properties without type hints
  • testHasTypeForUnionTypes() - test with int|string, string|null
  • testHasTypeForIntersectionTypes() - test with intersection types (if PHP version supports)

Type retrieval:

  • testGetTypeForSimpleTypes() - verify type normalization (booleanbool, etc.)
  • testGetTypeThrowsForCompositeTypes() - verify exception for union/intersection types
  • testGetTypesReturnsVector() - verify multiple types are returned as Vector
  • testGetFullyDescribedType() - verify full type string with | or & separators

Nullable detection (CRITICAL):

  • testIsNullableWithQuestionMark() - test ?int syntax
  • testIsNullableWithUnionNull() - test int|null syntax
  • testIsNullableWithDocComment() - test @var int|null PHPDoc
  • testIsNullableReturnsFalseForNonNullable() - test non-nullable properties

Edge cases:

  • testNonExistentPropertyThrowsException() - verify exceptions for missing properties
  • testStaticFromMethod() - verify CodeProperty::from() constructor

2. Create Test Fixtures

Create test classes in the test file with various property configurations:

class PropertyTestFixture {
    public int $intProperty;
    public ?string $nullableProperty;
    public int|string $unionProperty;
    public $noTypeHint;
    
    /**
     * @var float|null
     */
    public float $docNullableProperty;
}

3. Validate and Fix Implementation

While writing tests, validate these TODOs from the commit message:

  • Check nullable types behavior in hasType() / getType() / getTypes() / getFullyDescribedType()
  • Validate expectation that a ?int property is considered as int|null
  • Ensure Type::normalizeType() is consistently used

4. Documentation

  • Add class-level PHPDoc to CodeProperty explaining its purpose and usage
  • Add code examples to the docblock showing common use cases
  • Update method docblocks with @param and @return tags where missing

Getting started

https://agora.nasqueron.org/How_to_contribute_code will help you to start.

Clone the rKERUALD repository then use:

git switch develop/omnitools-1.x
arc patch D3860

Expected outcome

  • Comprehensive unit test coverage for CodeProperty class
  • All tests passing with >90% code coverage
  • Any bugs discovered during testing are fixed
  • Documentation is complete and clear

Resources

Questions?

Feel free to ask questions in the comments or reach out if you need clarification on any aspect of this task!

Related Objects

Event Timeline

dereckson triaged this task as Normal priority.Sun, Nov 9, 22:13
dereckson created this task.
dereckson moved this task from Backlog to Dev on the good-first-issue board.