Skip to content
This repository has been archived by the owner on Dec 3, 2023. It is now read-only.

[config-transformer] PHP Constants not transforming correctly, invalid PHP generated #4436

Open
tacman opened this issue Oct 17, 2022 · 4 comments

Comments

@tacman
Copy link
Contributor

tacman commented Oct 17, 2022

I used PHP constants in my yaml files to avoid typos in specific strings. It was a thing for a while, but never caught on, in part because of how enormously ugly it was.

Here's a yaml workflow

framework:
    workflows:
        Drawing:
            type: state_machine
            audit_trail:
                enabled: true
            marking_store:
                property: status
            supports: App\Entity\Drawing
            transitions:
                !php/const App\Entity\Drawing::TRANSITION_UPLOAD:
                    from:
                        - !php/const App\Entity\Drawing::PLACE_NEW
                    to: !php/const App\Entity\Drawing::PLACE_UPLOADED
                    metadata:
                        label: Upload
                        description: ''
                !php/const App\Entity\Drawing::TRANSITION_RENAME:
                    from:
                        - !php/const App\Entity\Drawing::PLACE_UPLOADED
                    to: !php/const App\Entity\Drawing::PLACE_RENAMED
                    metadata:
                        label: Rename
                        description: ''
                !php/const App\Entity\Drawing::TRANSITION_RESET:
                    from:
                        - !php/const App\Entity\Drawing::PLACE_UPLOADED
                        - !php/const App\Entity\Drawing::PLACE_RENAMED
                    to: !php/const App\Entity\Drawing::PLACE_NEW
                    metadata:
                        label: Reset
                        description: ''
            initial_marking: !php/const App\Entity\Drawing::PLACE_NEW
            places:
                !php/const App\Entity\Drawing::PLACE_NEW:
                    metadata:
                        label: New
                        description: ''
                !php/const App\Entity\Drawing::PLACE_UPLOADED:
                    metadata:
                        label: Uploaded
                        description: ''
                !php/const App\Entity\Drawing::PLACE_RENAMED:
                    metadata:
                        label: Renamed
                        description: ''

When I run

vendor/bin/config-transformer switch-format config/packages/workflow_Drawing.yaml 

The resulting PHP is invalid, and has enormously long lines, which ecs can't fix because the PHP is invalid (an unwanted colon on the end of the constant)

use App\Entity\Drawing;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
    $containerConfigurator->extension('framework', ['workflows' => ['Drawing' => ['type' => 'state_machine', 'audit_trail' => ['enabled' => true], 'marking_store' => ['property' => 'status'], 'supports' => Drawing::class, 'transitions' => Drawing::TRANSITION_UPLOAD:, 'initial_marking' => Drawing::PLACE_NEW, 'places' => Drawing::PLACE_NEW:]]]);
};

FWIW, the class has the constants defined as expected:

#[ORM\Entity(repositoryClass: DrawingRepository::class)]
class Drawing 
{
    final const PLACE_NEW = 'new';
    final const PLACE_UPLOADED = 'uploaded';
    final const PLACE_RENAMED= 'renamed';
    final const TRANSITION_UPLOAD= 'upload';
    final const TRANSITION_RENAME= 'rename';
    final const TRANSITION_RESET = 'reset';

image

@tacman
Copy link
Contributor Author

tacman commented Oct 17, 2022

Actually, it's not just a simple extra colon, a lot of the information isn't converted.

Even after manually fixing the syntax (the extra colon), it's not a valid configuration response.

<?php

declare(strict_types=1);

use App\Entity\Drawing;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
    $containerConfigurator->extension('framework', [
        'workflows' => [
            'Drawing' => [
                'type' => 'state_machine',
                'audit_trail' => ['enabled' => true],
                'marking_store' => ['property' => 'status'],
                'supports' => Drawing::class,
                'transitions' => Drawing::TRANSITION_UPLOAD,
                'initial_marking' => Drawing::PLACE_NEW,
                'places' => Drawing::PLACE_NEW
            ]]]);
};

@TomasVotruba
Copy link
Member

TomasVotruba commented Oct 18, 2022

Thanks for reporting 👍

We'll need minimalistics failing test PR for a start :)

@tacman
Copy link
Contributor Author

tacman commented Oct 18, 2022

Yeah, I was hoping you had the --debug option like you do for ecs and rector, to make reporting easier!

Absent that, is there a template to follow?

@TomasVotruba
Copy link
Member

You can copy this file and add the snippet:

https://github.com/symplify/symplify/blob/main/packages/config-transformer/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/constant.yaml

Ideally just 2 lines with single broken item.

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

No branches or pull requests

2 participants