php-generator icon indicating copy to clipboard operation
php-generator copied to clipboard

class->addProperty()->setType() adds a default namespace \ at the beggining to the property type

Open ppazos opened this issue 1 year ago • 1 comments

Version: 4.1.5

Bug Description

I generated a class, and added a property which type is not a basic PHP type, it's another class.

When I echo the PhpFile, the type has a \ prefix.

<?php

namespace org\openehr\base\base_types\identification;

use todo\ns\to\propertyclass\String;
use todo\ns\to\propertyclass\UID_BASED_ID;

class LOCATABLE_REF extends \todo\ns\path\OBJECT_REF
{
	public String $path;
	public \UID_BASED_ID $id;     <<<<< I expect UID_BASE_ID not \UID_BASE_ID
}

Steps To Reproduce

$phpFile = new \Nette\PhpGenerator\PhpFile;

$classWithoutGenerics = 'LOCATABLE_REF';

$phpNamespace = 'org\openehr\base\base_types\identification';
$genNS = $phpFile->addNamespace($phpNamespace);

$genClass = $genNS->addClass($classWithoutGenerics);

$genClass->setExtends('todo\\ns\\path\\OBJECT_REF');

$genProperty = $genClass->addProperty('id');
$genNS->addUse('todo\\ns\\to\\propertyclass\\UID_BASED_ID');
$genProperty->setType('UID_BASED_ID');

Expected Behavior

I expect UID_BASE_ID not \UID_BASE_ID

Possible Solution

Nope, I don't know where the \ gets added to the type.

ppazos avatar Aug 30 '24 03:08 ppazos

NOTE: this is what is adding the \:

https://github.com/nette/php-generator/blob/master/src/PhpGenerator/Printer.php#L401

The type enters without the prefix backslash and that line adds it, not sure why.

ppazos avatar Aug 30 '24 03:08 ppazos

See this https://github.com/nette/php-generator?tab=readme-ov-file#class-names-resolving

dg avatar Sep 01 '24 22:09 dg