reliese / laravel

Reliese Laravel Model Generator
MIT License
1.49k stars 316 forks source link

Incorrect type-hints on properties when using base classes #278

Closed finiteinfinity closed 6 months ago

finiteinfinity commented 6 months ago

When using base classes, the generated type-hints for relationships can be incorrectly typed as the base class rather than the child class.

This only seems to happen when you have 2 properties named which contain a like-string, e.g.

/**
 * @property User $manager
 * @property UserType $userType
 */
class User extends Model
{
}

During template construction (\Reliese\Coders\Model\Factory, specifically line 275) when the dependency imports are being shortened, the use of str_replace causes all instances of the User FQCN string to be replaced with the imported version. This unintentionally picks up more than the 1 instance it should be replacing for the import. A simple fix would be to use preg_replace instead with a word boundary (\b).