phpunit-architecture-test icon indicating copy to clipboard operation
phpunit-architecture-test copied to clipboard

Update ObjectDependenciesDescription.php

Open giorgiopogliani opened this issue 10 months ago • 3 comments

Fixing error: Typed property PHPUnit\Architecture\Asserts\Dependencies\ObjectDependenciesDescription::$uses must not be accessed before initialization

giorgiopogliani avatar Jan 31 '25 16:01 giorgiopogliani

@giorgiopogliani can you write an example here to reproduce this error? please

ta-tikoma avatar Feb 03 '25 10:02 ta-tikoma

I was using pestphp/pest on a laravel project, setting up arch tests cause this error if some packages are installed.

In pestphp/pest repo they closed the issue shifting the blame on the packages, but with the chages I am proposing all my tests suite was fixed.

i am not sure the root cause of the error and why some packages installed cause this and other packages don't.

If you want to replicate the error with one of the dep I found you can with these steps:

  • Install new laravel proejct composer create-project laravel/laravel test
  • Install pest 3: composer require pestphp/pest:^3 --dev -W
  • Install a dep I found: composer require mateusjunges/laravel-trackable-jobs
  • Init pest: vendor/bin/pest --init
  • Replace everything inside Unit/ExampleTest.php with for example: arch()->preset()->php();
  • Run: vendor/bin/pest
  • You should see: " Typed property PHPUnit\Architecture\Asserts\Dependencies\ObjectDependenciesDescription::$uses must not be accessed before initialization"

giorgiopogliani avatar Feb 03 '25 10:02 giorgiopogliani

@giorgiopogliani I try to fix it https://github.com/pestphp/pest-plugin-arch/pull/22

ta-tikoma avatar Feb 07 '25 09:02 ta-tikoma

Fixing error: Typed property PHPUnit\Architecture\Asserts\Dependencies\ObjectDependenciesDescription::$uses must not be accessed before initialization

I had the same exact error and applying your snippet fixed the issue Thank you. Then i got similar error at abstract class ObjectDescriptionBase and following your pattern i fixed as below

public function __construct() { $this->reflectionClass = new ReflectionClass($this); $this->type = new ObjectType($this->reflectionClass->getShortName()); $this->path = $this->reflectionClass->getFileName(); $this->name = $this->reflectionClass->getName(); $this->stmts = ServiceContainer::$nodeTraverser->traverse($this->reflectionClass->getAST()); }

momostafa avatar Apr 06 '25 06:04 momostafa

@momostafa , please try v3.1.0 and tell us if you still have the error?

ta-tikoma avatar Apr 10 '25 05:04 ta-tikoma

@momostafa , please try v3.1.0 and tell us if you still have the error?

I will do that. Thank you

momostafa avatar Apr 12 '25 00:04 momostafa