Skip to content

Conversation

Superkooka
Copy link
Contributor

@Superkooka Superkooka commented Oct 22, 2020

Edit: now this flag is global and you can defined if you want to use typing in the configuraiton file 🥳 Just one test dosn't pass.
When it's fix, this PR is ready to be merge.


Hi 👋
With PHP 7.4 and the arrival of PHP 8.0 typing is starting to gain importance. So it's why I have adding the --typed flag. This flag type the properties of your entity.

For exemple, with PHP 7.4 or more, if I create this entity, I obtain the PHP below:

PS D:\symfony\sf-maker> php .\bin\console make:entity --typed

 Class name of the entity to create or update (e.g. AgreeableKangaroo):
 > TypedPizza
TypedPizza

 created: src/Entity/TypedPizza.php
 created: src/Repository/TypedPizzaRepository.php

 Entity generated! Now let's add some fields!
 You can always add more fields later manually or by re-running this command.

 New property name (press <return> to stop adding fields):
 > isCooked

 Field type (enter ? to see all types) [boolean]:
 >


 Can this field be null in the database (nullable) (yes/no) [no]:
 >

 updated: src/Entity/TypedPizza.php

 Add another property? Enter the property name (or press <return> to stop adding fields):
 >



  Success!


 Next: When you're ready, create a migration with php bin/console make:migration
<?php

namespace App\Entity;

use App\Repository\TypedPizzaRepository;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass=TypedPizzaRepository::class)
 */
class TypedPizza
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="boolean")
     */
    private ?bool $isCooked;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getIsCooked(): ?bool
    {
        return $this->isCooked;
    }

    public function setIsCooked(bool $isCooked): self
    {
        $this->isCooked = $isCooked;

        return $this;
    }
}

If I execute this command with a lower PHP version I have a confirmation warning to ask if I really want typed properties.
To run the test, this pr need PHP 7.4 inot the CI, see #690

@weaverryan
Copy link
Member

Hi @Superkooka!

Ah, thank you for this! It's been on my mind lately!!! ❤️

2 quick things come to mind:

A) If someone is using PHP 7.4, when would they not want to use typed properties? I'm wondering about the option: do we need it? Or, do you "envision" the question as a "sanity" check to make sure they aren't accidentally using php 7.4 on a code base that will actually use 7.3 (for example) on production?

B) There are various other spots where we should also use typed properties. A simple - but probably not exhaustive - thing to check for is to search this bundle for addEntityField(). That method is also called in EntityRegenerator and UserClassBuilder. This is part of the reason why having an option on a command feels odd to me. We would need to add the option on other commands too. But really, you probably either want (or don't want) typed properties globally. So a global option (or just detecting PHP 7.4) might be better.

Thanks!

@duboiss
Copy link
Contributor

duboiss commented Oct 23, 2020

Hey @weaverryan

Regarding behavior, what do you think of this? For all the commands that may need to generate class properties.

  1. By checking the PHP required version in composer.json, if >= 7.4, we type automatically.

  2. If this PHP required version is lower than 7.4 but the PHP interpreter version (in the path) is >= 7.4, asking for type.
    If user says yes, warn him to update his composer.json with a minimum of 7.4.

  3. Else, is that both versions are lower than 7.4, we don't type.

Second, for editing, what to base it on? Same behavior? Make:entity is the only command for "update" a php class ?

Next, yes it should concern the whole bundle and not just make:entity. What would you recommend for this at the files level ? A new "Util" file ?

Thanks ;)

@Superkooka Superkooka changed the title Feat: adding the possibility to type properties on entity with --typed Feat: Type properties with PHP >= 7.4 Oct 23, 2020
Copy link

@innocenzi innocenzi left a comment

Choose a reason for hiding this comment

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

Here are a few suggestions about the code style, and a few English fixes as well 👍

@Superkooka Superkooka changed the title Feat: Type properties with PHP >= 7.4 [WIP] Feat: Type properties with PHP >= 7.4 Oct 23, 2020
@jrushlow jrushlow added the Status: Needs Work Additional work is needed label Oct 27, 2020
@Superkooka
Copy link
Contributor Author

To avoid all the problems with the different versions of php (<7.4) I decided to use a global flag (--typed) as well as the possibility of activating this flag by default in a configuration file as long as the version minimum PHP supported by the maker bundle will not be >= 7.4. In the following day(s) I will rebase and force push on my branch to keep a clean history of commits

@weaverryan weaverryan changed the base branch from master to main November 16, 2020 20:00
@Superkooka
Copy link
Contributor Author

Superkooka commented Nov 19, 2020

Hi 👋 ! I don't have the time to fix for passing the CI, so if someone can fix it, I just have to squash the commit and this PR is ready to merge.

@aleksblendwerk
Copy link

Seems the tests pass so far but the whole job was just cancelled after 10 minutes before they all finished.

Could the tests be triggered again so we can see if there's still anything actually wrong?

@Superkooka
Copy link
Contributor Author

Just look to the "CI / PHP 7.1.3 + symfony/skeleton@stable" job, the other are automatically cancel after.

@aleksblendwerk
Copy link

aleksblendwerk commented Dec 13, 2020

Just look to the "CI / PHP 7.1.3 + symfony/skeleton@stable" job, the other are automatically cancel after.

That 7.1.3 run has tests skipped that are OK on 7.4 though. I doubt 7.1 is the top priority here...

@Superkooka
Copy link
Contributor Author

I think if the CI crash in 7.1, it's also crash on 7.4, the 7.1 is always supported and I don't know how to run only one test 😄

@jrushlow
Copy link
Collaborator

Hi @Superkooka great work so far! Just a heads up in PR #736 I've created a canUseTypedProperties(): bool method, https://github.com/symfony/maker-bundle/blob/44278a695902c491086ecc94e5d9a2bf1f1ccd80/src/Util/PhpCompatUtil.php

This is not merged yet but should be shortly. Once it's merged, I think rather than setting a flag - you'll be able to check the newly created $use_typed_properties global variable that will be available when creating templates. e.g.
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Needs Work Additional work is needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants