Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Fixed class generator ignoring use aliases for extended class #144

Conversation

allcode
Copy link
Contributor

@allcode allcode commented Nov 11, 2017

This PR contains a fix for the class generator when using a use alias for the extended
class.

Bug explanation

Before this patch, the class generator would ignore all use aliases. This resulted
in an incorrect extends class. For example:

$classGenerator = new ClassGenerator();
$classGenerator->setName('ClassName');
$classGenerator->setNamespaceName('SomeNamespace');
$classGenerator->addUse('Foo\\Bar', 'BarAlias');
$classGenerator->setExtendedClass('BarAlias');
echo $classGenerator->generate();

would result in:

namespace SomeNamespace;

use Foo\Bar as BarAlias;

class ClassName extends \BarAlias {}

When using an FQCN for the extended class, it would ignore the use statement:

$classGenerator = new ClassGenerator();
$classGenerator->setName('ClassName');
$classGenerator->setNamespaceName('SomeNamespace');
$classGenerator->addUse('Foo\\Bar', 'BarAlias');
$classGenerator->setExtendedClass('Foo\\Bar');
echo $classGenerator->generate();

would result in:

namespace SomeNamespace;

use Foo\Bar as BarAlias;

class ClassName extends \Foo\Bar {}

Fix explanation

This PR fixes the ClassGenerator by checking whether the extended class matches
one of the use statements.
It supports the following varieties:

Using an alias as the extended class

$classGenerator->setName('ClassName');
$classGenerator->setNamespaceName('SomeNamespace');
$classGenerator->addUse('Foo\\Bar', 'BarAlias');
$classGenerator->setExtendedClass('BarAlias');

will result in:

namespace SomeNamespace;

use Foo\Bar as BarAlias;

class ClassName extends BarAlias {}

Using an alias of the extended class namespace

$classGenerator->setName('ClassName');
$classGenerator->setNamespaceName('SomeNamespace');
$classGenerator->addUse('Foo\\Bar', 'BarAlias');
$classGenerator->setExtendedClass('BarAlias\\FooBar');

will result in:

namespace SomeNamespace;

use Foo\Bar as BarAlias;

class ClassName extends BarAlias\FooBar {}

Using an FQCN as the extended class

The class generator will replace the extended class with its alias, if an exact use
statement match is defined.

$classGenerator->setName('ClassName');
$classGenerator->setNamespaceName('SomeNamespace');
$classGenerator->addUse('Foo\\Bar', 'BarAlias');
$classGenerator->setExtendedClass('Foo\\Bar');

will result in:

namespace SomeNamespace;

use Foo\Bar as BarAlias;

class ClassName extends BarAlias {}

The class generator will replace the namespace of the extended class with its alias,
if an exact use statement match is defined.

$classGenerator->setName('ClassName');
$classGenerator->setNamespaceName('SomeNamespace');
$classGenerator->addUse('Foo\\Bar', 'BarAlias');
$classGenerator->setExtendedClass('Foo\\Bar\\FooBar');

will result in:

namespace SomeNamespace;

use Foo\Bar as BarAlias;

class ClassName extends BarAlias\FooBar {}

… extended class

The class generator will now use the alias of a use statement if
it matches the extended class or its namespace.
@allcode allcode force-pushed the bugfix/fix-class-extends-with-use-alias branch from 54e2bc5 to d45d476 Compare November 11, 2017 16:26
allcode added a commit to allcode/xsd2php that referenced this pull request Nov 14, 2017
- Removed default mixed parameter type, as zend code generator will now
  see mixed as a class name.
- Removed aliasing of extended class when extended class name is the
  same as the generated class; zend code generator currently has a
  bug with aliases
  (see pull request for fix: zendframework/zend-code#144).
weierophinney added a commit that referenced this pull request Aug 13, 2018
weierophinney added a commit that referenced this pull request Aug 13, 2018
@weierophinney
Copy link
Member

Thanks, @allcode!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants