Skip to content

(Update #589) Replace Generator by EntityClassGenerator in MakerResetPassword #590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from Jun 12, 2020
Merged

Conversation

ghost
Copy link

@ghost ghost commented Apr 17, 2020

UPDATE PULL REQUEST #589

Q A
Branch? master
Bug fix? no
New feature? - yes (internal)
Deprecations? - no
Tickets - no
License MIT
Doc PR - no need

At the request of Ryan Weaver.

I replaced the use of Generator by EntityClassGenerator.

We then needed to use ClassSourceManipulator and i propose to add 3 methods to this class, tests included.

$manipulator->clearClassNodeStmts()
Very short method to remove all Node\Stmts in the class and update the source code.

$manipulator->addTrait()
Add a use Trait; at first in the class.

$manipulator->addConstructor()
Is a shortcut to add a constructor in the right place.

I also propose to add two optional parameters to the $manipulator->addMethodBuilder() method: array $params = [], string $methodBody = null;

Allowing to build methods more directly when needed.

Copy link
Member

@weaverryan weaverryan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor things - keep going! :)

);
$this->fileManager->dumpFile($pathRequestRepository, $manipulator->getSourceCode());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think putting all of this new code into a private method in this class might be nice. Or, its own class, which could then be unit tested a bit easier - like UserClassBuilder

Copy link
Author

@ghost ghost May 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a new private method like you said first because i think this stuff is really specific to this command with a good test coverage. See if you like :)

Copy link
Member

@weaverryan weaverryan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getting close!

'@ORM\Id()',
'@ORM\GeneratedValue()',
'@ORM\Column(type="integer")',
]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is not needed anymore. And ClassSourceManipulator is just ignoring it because it's already there.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -19,8 +19,11 @@ abstract class BaseRelation
private $propertyName;
private $targetClassName;
private $targetPropertyName;
private $returnType;
private $returnTypeIsNullable = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private $returnTypeIsNullable = true;
private $returnTypeIsNullable;

I just don't want this to have a default value... so let's leave it null.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And maybe we should call these properties:overriddenReturnType and isOverriddenReturnTypeNullable... because that's what they really are. If they are not set, then it means that the "default" return type of the related class would be used.

Copy link
Member

@weaverryan weaverryan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor stuff left!

composer.json Outdated
"branch-alias": {
"dev-master": "1.0-dev"
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be reverted - your editor may have done this automatically

public function isOverriddenReturnTypeNullable(): bool
{
return $this->overriddenReturnType;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I may have confused you. Here is how I would expect the new parts of this class to look (I'm also changing the name "overridden" to "custom"):

private $customReturnType;
private $isCustomReturnTypeNullable;

public function getCustomReturnType(): ?string
{
    return $this->customReturnType;
}

public function isCustomReturnTypeNullable(): bool
{
    return $this->isCustomReturnTypeNullable;
}

public function setCustomReturnType(string $customReturnType, bool $isNullable): void
{
    $this->customReturnType = $customReturnType;
    $this->isCustomReturnTypeNullable = $isNullable;
}

Then, in ClassSourceManipulator, when we use this, the code would be:

        $this->addGetter(
            $relation->getPropertyName(),
            $typeHint,
            $relation->getCustomReturnType() ?: $typeHint,
            // getter methods always have nullable return values
            // unless this has been customized explicitly
            $relation->getCustomReturnType() ? $relation->isCustomReturnTypeNullable() : $typeHint,
            $relation->isOverriddenReturnTypeNullable()
        );

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now, without modifying other parts of the MakerBundle, i think we should do things like this in BaseRelation

    private $customReturnType;
    private $isCustomReturnTypeNullable = true;

    public function isCustomReturnTypeNullable(): bool
    {
        return $this->isCustomReturnTypeNullable;
    }


    public function setCustomReturnType(string $customReturnType, bool $isNullable)
    {
        $this->customReturnType = $customReturnType;
        $this->isCustomReturnTypeNullable = $isNullable;

        return $this;
    }

Copy link
Author

@ghost ghost May 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this in ClassSourceManipulator

$this->addGetter(
            $relation->getPropertyName(),
            $relation->getCustomReturnType() ?: $typeHint,
            // getter methods always have nullable return values
            // unless this has been customized explicitly
            $relation->isCustomReturnTypeNullable()
        );

return $this->isNullable;
}

return false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think (?) this isn't needed. It's true that isNullable could be null, but the :bool return type will cast null into a false boolean.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


$manipulator->addManyToOneRelation((new RelationManyToOne())
->setPropertyName('user')
->setTargetClassName("App\Entity\User") // customization fail in php 7.3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the problem in PHP 7.3? We need to be explicit. And if there IS a problem currently in 7.3, then that's a block for the PR and we should be clear that this is still a "TODO" (like by posting a comment about it in the PR or in the PR description)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not php 7.3, i wrote that because only the test for php 7.3 failed in travis.

$this->assertStringContainsString('@ORM\ManyToOne(targetEntity="App\Entity\UserCustom")', $contentResetPasswordRequest);

Copy link
Author

@ghost ghost May 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jrushlow added new code. Our targetClass is generaated as a fetch class UserCustom::class not in FQCN "App\Entity\UserCustom"

<?php
parent::__construct($registry, ResetPasswordRequest::class);
CODE
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we talked about, if we want to avoid the comments in the repository class, let's pass an argument to EntityClassGenerator::generateEntityClass() to make it not generate the comments. That method name is getting a little long, but that's ok - it's an internal class.

{
$this->getClassNode()->stmts = [];
$this->updateSourceCodeFromNewStmts();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hopefully this won't be needed at the end :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes :)

Copy link
Member

@weaverryan weaverryan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERY close - I'm quite pleased with this PR

@ghost ghost closed this Jun 5, 2020
@ghost ghost reopened this Jun 7, 2020
@ghost ghost changed the title Update PR make reset password (Update PR make reset password) Replace Generator by EntityClassGenerator in MakerResetPassword Jun 9, 2020
@ghost ghost changed the title (Update PR make reset password) Replace Generator by EntityClassGenerator in MakerResetPassword (Update #589) Replace Generator by EntityClassGenerator in MakerResetPassword Jun 9, 2020
@weaverryan
Copy link
Member

Thank you @archeoprog!

@weaverryan weaverryan merged commit 4705242 into symfony:master Jun 12, 2020
weaverryan added a commit that referenced this pull request Feb 1, 2021
…rushlow)

This PR was squashed before being merged into the 1.0-dev branch.

Discussion
----------

[Internal] [User] removed unused service from make:user

PR #590 added `DoctrineHelper` to `MakeResetPassword` but the service is never used.

Commits
-------

a49d6f2 [Internal] [User] removed unused service from make:user
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant