[config-transformer] PHP Constants not transforming correctly, invalid PHP generated
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';

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
]]]);
};
Thanks for reporting :+1:
We'll need minimalistics failing test PR for a start :)
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?
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.