Skip to content

Fix existing inline PHP snippets#3206

Draft
mnocon wants to merge 18 commits into
test-php-snippetsfrom
fix-php-snippets
Draft

Fix existing inline PHP snippets#3206
mnocon wants to merge 18 commits into
test-php-snippetsfrom
fix-php-snippets

Conversation

@mnocon
Copy link
Copy Markdown
Contributor

@mnocon mnocon commented May 17, 2026

This PR fixes the existing inlined PHP code samples, using the following tools:

  • PHPStan
  • Rector
  • CS-Fixer
  • Deptrac

To avoid triggering the PHPStan baselined error every time the Markdown content around a code sample changes, the line-numbered files are renamed to use sha-256 as the filename.
This way, PHPStan correctly remembers the checked file even if the content around (and thus, line numbers) change.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 17, 2026

Preview of modified files: Too many files modified in a single PR, preview link list is skipped. (298 files > 100)

@mnocon mnocon force-pushed the fix-php-snippets branch from d7df236 to 5150030 Compare May 17, 2026 14:18
@github-actions
Copy link
Copy Markdown

code_samples/ change report

Before (on target branch)After (in current PR)

code_samples/api/graphql/src/GraphQL/Schema/MyFieldDefinitionMapper.php

docs/api/graphql/graphql_custom_ft.md@62:``` php

code_samples/api/graphql/src/GraphQL/Schema/MyFieldDefinitionMapper.php

docs/api/graphql/graphql_custom_ft.md@62:``` php
docs/api/graphql/graphql_custom_ft.md@63:[[= include_file('code_samples/api/graphql/src/GraphQL/Schema/MyFieldDefinitionMapper.php', 0, 16) =]][[= include_file('code_samples/api/graphql/src/GraphQL/Schema/MyFieldDefinitionMapper.php', 36, 37) =]]
docs/api/graphql/graphql_custom_ft.md@64:```
docs/api/graphql/graphql_custom_ft.md@63:[[= include_code('code_samples/api/graphql/src/GraphQL/Schema/MyFieldDefinitionMapper.php', 1, 17) =]]
docs/api/graphql/graphql_custom_ft.md@64:[[= include_code('code_samples/api/graphql/src/GraphQL/Schema/MyFieldDefinitionMapper.php', 38, 39) =]]
docs/api/graphql/graphql_custom_ft.md@65:```

001⫶<?php declare(strict_types=1);
002⫶
003⫶namespace App\GraphQL\Schema;
004⫶
005⫶use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType;
006⫶use Ibexa\Contracts\Core\Repository\Values\ContentType\FieldDefinition;
007⫶use Ibexa\Contracts\GraphQL\Schema\Domain\Content\Mapper\FieldDefinition\FieldDefinitionMapper;
008⫶use Ibexa\GraphQL\Schema\Domain\Content\Mapper\FieldDefinition\DecoratingFieldDefinitionMapper;
009⫶use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
010⫶
011⫶class MyFieldDefinitionMapper extends DecoratingFieldDefinitionMapper implements FieldDefinitionMapper
012⫶{
013⫶ protected function getFieldTypeIdentifier(): string
014⫶ {
015⫶ return 'my_field_type';
016⫶ }

001⫶<?php declare(strict_types=1);
002⫶
003⫶namespace App\GraphQL\Schema;
004⫶
005⫶use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType;
006⫶use Ibexa\Contracts\Core\Repository\Values\ContentType\FieldDefinition;
007⫶use Ibexa\Contracts\GraphQL\Schema\Domain\Content\Mapper\FieldDefinition\FieldDefinitionMapper;
008⫶use Ibexa\GraphQL\Schema\Domain\Content\Mapper\FieldDefinition\DecoratingFieldDefinitionMapper;
009⫶use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
010⫶
011⫶class MyFieldDefinitionMapper extends DecoratingFieldDefinitionMapper implements FieldDefinitionMapper
012⫶{
013⫶ protected function getFieldTypeIdentifier(): string
014⫶ {
015⫶ return 'my_field_type';
016⫶ }
017⫶}
017⫶
018⫶}


docs/api/graphql/graphql_custom_ft.md@77:```php
docs/api/graphql/graphql_custom_ft.md@78:[[= include_file('code_samples/api/graphql/src/GraphQL/Schema/MyFieldDefinitionMapper.php', 19, 22, remove_indent=True) =]]
docs/api/graphql/graphql_custom_ft.md@79:```
docs/api/graphql/graphql_custom_ft.md@78:```php
docs/api/graphql/graphql_custom_ft.md@79:[[= include_code('code_samples/api/graphql/src/GraphQL/Schema/MyFieldDefinitionMapper.php', 21, 24, remove_indent=True) =]]
docs/api/graphql/graphql_custom_ft.md@80:```

001⫶if (!$this->canMap($fieldDefinition)) {
002⫶ return parent::mapToFieldValueInputType($contentType, $fieldDefinition);
003⫶}


001⫶if (!$this->canMap($fieldDefinition)) {
002⫶ return parent::mapToFieldValueInputType($contentType, $fieldDefinition);
003⫶}

docs/api/graphql/graphql_custom_ft.md@101:```php
docs/api/graphql/graphql_custom_ft.md@102:[[= include_file('code_samples/api/graphql/src/GraphQL/Schema/MyFieldDefinitionMapper.php') =]]
docs/api/graphql/graphql_custom_ft.md@103:```
docs/api/graphql/graphql_custom_ft.md@102:```php
docs/api/graphql/graphql_custom_ft.md@103:[[= include_file('code_samples/api/graphql/src/GraphQL/Schema/MyFieldDefinitionMapper.php') =]]
docs/api/graphql/graphql_custom_ft.md@104:```

001⫶<?php declare(strict_types=1);
002⫶
003⫶namespace App\GraphQL\Schema;
004⫶
005⫶use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType;
006⫶use Ibexa\Contracts\Core\Repository\Values\ContentType\FieldDefinition;
007⫶use Ibexa\Contracts\GraphQL\Schema\Domain\Content\Mapper\FieldDefinition\FieldDefinitionMapper;
008⫶use Ibexa\GraphQL\Schema\Domain\Content\Mapper\FieldDefinition\DecoratingFieldDefinitionMapper;
009⫶use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
010⫶
011⫶class MyFieldDefinitionMapper extends DecoratingFieldDefinitionMapper implements FieldDefinitionMapper
012⫶{
013⫶ protected function getFieldTypeIdentifier(): string
014⫶ {
015⫶ return 'my_field_type';
016⫶ }
017⫶

001⫶<?php declare(strict_types=1);
002⫶
003⫶namespace App\GraphQL\Schema;
004⫶
005⫶use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType;
006⫶use Ibexa\Contracts\Core\Repository\Values\ContentType\FieldDefinition;
007⫶use Ibexa\Contracts\GraphQL\Schema\Domain\Content\Mapper\FieldDefinition\FieldDefinitionMapper;
008⫶use Ibexa\GraphQL\Schema\Domain\Content\Mapper\FieldDefinition\DecoratingFieldDefinitionMapper;
009⫶use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
010⫶
011⫶class MyFieldDefinitionMapper extends DecoratingFieldDefinitionMapper implements FieldDefinitionMapper
012⫶{
013⫶ protected function getFieldTypeIdentifier(): string
014⫶ {
015⫶ return 'my_field_type';
016⫶ }
017⫶
018⫶    public function mapToFieldValueInputType(ContentType $contentType, FieldDefinition $fieldDefinition): ?string
019⫶ {
020⫶ if (!$this->canMap($fieldDefinition)) {
021⫶ return parent::mapToFieldValueInputType($contentType, $fieldDefinition);
022⫶ }
023⫶
024⫶ return $this->nameMyFieldInputType($contentType, $fieldDefinition);
025⫶ }
026⫶
027⫶ private function nameMyFieldInputType(ContentType $contentType, FieldDefinition $fieldDefinition): string
028⫶ {
029⫶ $converter = new CamelCaseToSnakeCaseNameConverter(null, false);
030⫶
031⫶ return sprintf(
032⫶ '%s%sInput',
033⫶ $converter->denormalize($contentType->identifier),
034⫶ $converter->denormalize($fieldDefinition->identifier)
035⫶ );
036⫶ }
037⫶}
018⫶    #[\Override]
019⫶ public function mapToFieldValueInputType(ContentType $contentType, FieldDefinition $fieldDefinition): ?string
020⫶ {
021⫶ if (!$this->canMap($fieldDefinition)) {
022⫶ return parent::mapToFieldValueInputType($contentType, $fieldDefinition);
023⫶ }
024⫶
025⫶ return $this->nameMyFieldInputType($contentType, $fieldDefinition);
026⫶ }
027⫶
028⫶ private function nameMyFieldInputType(ContentType $contentType, FieldDefinition $fieldDefinition): string
029⫶ {
030⫶ $converter = new CamelCaseToSnakeCaseNameConverter(null, false);
031⫶
032⫶ return sprintf(
033⫶ '%s%sInput',
034⫶ $converter->denormalize($contentType->identifier),
035⫶ $converter->denormalize($fieldDefinition->identifier)
036⫶ );
037⫶ }
038⫶}


code_samples/api/public_php_api/src/Command/SegmentCommand.php

docs/users/segment_api.md@17:``` php
docs/users/segment_api.md@18:[[= include_file('code_samples/api/public_php_api/src/Command/SegmentCommand.php', 48, 55) =]]
docs/users/segment_api.md@19:```

001⫶ $segmentGroup = $this->segmentationService->loadSegmentGroupByIdentifier('custom_group');
002⫶
003⫶ $segments = $this->segmentationService->loadSegmentsAssignedToGroup($segmentGroup);
004⫶
005⫶ foreach ($segments as $segment) {
006⫶ $output->writeln('Segment identifier: ' . $segment->getIdentifier() . ', name: ' . $segment->getName());
007⫶ }

docs/users/segment_api.md@23:``` php
docs/users/segment_api.md@24:[[= include_file('code_samples/api/public_php_api/src/Command/SegmentCommand.php', 56, 57) =]]
docs/users/segment_api.md@25:```

001⫶ $segment = $this->segmentationService->loadSegmentByIdentifier('segment_1');

docs/users/segment_api.md@31:``` php
docs/users/segment_api.md@32:[[= include_file('code_samples/api/public_php_api/src/Command/SegmentCommand.php', 60, 65) =]]
docs/users/segment_api.md@33:```

001⫶ $output->writeln((
002⫶ $this->segmentationService->isUserAssignedToSegment($user, $segment)
003⫶ ? 'The user is assigned to the segment.'
004⫶ : 'The user is not assigned to the segment.'
005⫶ ));

docs/users/segment_api.md@39:``` php
docs/users/segment_api.md@40:[[= include_file('code_samples/api/public_php_api/src/Command/SegmentCommand.php', 58, 59) =]]
docs/users/segment_api.md@41:```

001⫶ $this->segmentationService->assignUserToSegment($user, $segment);

docs/users/segment_api.md@49:``` php
docs/users/segment_api.md@50:[[= include_file('code_samples/api/public_php_api/src/Command/SegmentCommand.php', 32, 39) =]]
docs/users/segment_api.md@51:```

001⫶ $segmentGroupCreateStruct = new SegmentGroupCreateStruct([
002⫶ 'name' => 'Custom Group',
003⫶ 'identifier' => 'custom_group',
004⫶ 'createSegments' => [],
005⫶ ]);
006⫶
007⫶ $newSegmentGroup = $this->segmentationService->createSegmentGroup($segmentGroupCreateStruct);

docs/users/segment_api.md@55:``` php
docs/users/segment_api.md@56:[[= include_file('code_samples/api/public_php_api/src/Command/SegmentCommand.php', 40, 47) =]]
docs/users/segment_api.md@57:```

001⫶ $segmentCreateStruct = new SegmentCreateStruct([
002⫶ 'name' => 'Segment 1',
003⫶ 'identifier' => 'segment_1',
004⫶ 'group' => $newSegmentGroup,
005⫶ ]);
006⫶
007⫶ $newSegment = $this->segmentationService->createSegment($segmentCreateStruct);



code_samples/api/public_php_api/src/Command/SegmentCommand.php

docs/users/segment_api.md@17:``` php
docs/users/segment_api.md@18:[[= include_file('code_samples/api/public_php_api/src/Command/SegmentCommand.php', 48, 55) =]]
docs/users/segment_api.md@19:```

001⫶ $segmentGroup = $this->segmentationService->loadSegmentGroupByIdentifier('custom_group');
002⫶
003⫶ $segments = $this->segmentationService->loadSegmentsAssignedToGroup($segmentGroup);
004⫶
005⫶ foreach ($segments as $segment) {
006⫶ $output->writeln('Segment identifier: ' . $segment->getIdentifier() . ', name: ' . $segment->getName());
007⫶ }

docs/users/segment_api.md@23:``` php
docs/users/segment_api.md@24:[[= include_file('code_samples/api/public_php_api/src/Command/SegmentCommand.php', 56, 57) =]]
docs/users/segment_api.md@25:```

001⫶ $segment = $this->segmentationService->loadSegmentByIdentifier('segment_1');

docs/users/segment_api.md@31:``` php
docs/users/segment_api.md@32:[[= include_file('code_samples/api/public_php_api/src/Command/SegmentCommand.php', 60, 65) =]]
docs/users/segment_api.md@33:```

001⫶ $output->writeln((
002⫶ $this->segmentationService->isUserAssignedToSegment($user, $segment)
003⫶ ? 'The user is assigned to the segment.'
004⫶ : 'The user is not assigned to the segment.'
005⫶ ));

docs/users/segment_api.md@39:``` php
docs/users/segment_api.md@40:[[= include_file('code_samples/api/public_php_api/src/Command/SegmentCommand.php', 58, 59) =]]
docs/users/segment_api.md@41:```

001⫶ $this->segmentationService->assignUserToSegment($user, $segment);

docs/users/segment_api.md@49:``` php
docs/users/segment_api.md@50:[[= include_file('code_samples/api/public_php_api/src/Command/SegmentCommand.php', 32, 39) =]]
docs/users/segment_api.md@51:```

001⫶ $segmentGroupCreateStruct = new SegmentGroupCreateStruct([
002⫶ 'name' => 'Custom Group',
003⫶ 'identifier' => 'custom_group',
004⫶ 'createSegments' => [],
005⫶ ]);
006⫶
007⫶ $newSegmentGroup = $this->segmentationService->createSegmentGroup($segmentGroupCreateStruct);

docs/users/segment_api.md@55:``` php
docs/users/segment_api.md@56:[[= include_file('code_samples/api/public_php_api/src/Command/SegmentCommand.php', 40, 47) =]]
docs/users/segment_api.md@57:```

001⫶ $segmentCreateStruct = new SegmentCreateStruct([
002⫶ 'name' => 'Segment 1',
003⫶ 'identifier' => 'segment_1',
004⫶ 'group' => $newSegmentGroup,
005⫶ ]);
006⫶
007⫶ $newSegment = $this->segmentationService->createSegment($segmentCreateStruct);

docs/users/segment_api.md@67:``` php
docs/users/segment_api.md@68:[[= include_code('code_samples/api/public_php_api/src/Command/SegmentCommand.php', 67, 67, remove_indent=True) =]]
docs/users/segment_api.md@69:```

001⫶$this->segmentationService->removeSegmentGroup($segmentGroup);


code_samples/catalog/custom_catalog_filter/src/CatalogFilter/DataTransformer/ProductNameCriterionTransformer.php

docs/product_catalog/create_custom_catalog_filter.md@50:``` php
docs/product_catalog/create_custom_catalog_filter.md@51:[[= include_file('code_samples/catalog/custom_catalog_filter/src/CatalogFilter/DataTransformer/ProductNameCriterionTransformer.php') =]]
docs/product_catalog/create_custom_catalog_filter.md@52:```

001⫶<?php
002⫶
003⫶declare(strict_types=1);
004⫶
005⫶namespace App\CatalogFilter\DataTransformer;
006⫶
007⫶use Ibexa\Contracts\ProductCatalog\Values\Product\Query\Criterion\ProductName;
008⫶use Symfony\Component\Form\DataTransformerInterface;
009⫶use Symfony\Component\Form\Exception\TransformationFailedException;
010⫶

code_samples/catalog/custom_catalog_filter/src/CatalogFilter/DataTransformer/ProductNameCriterionTransformer.php

docs/product_catalog/create_custom_catalog_filter.md@50:``` php
docs/product_catalog/create_custom_catalog_filter.md@51:[[= include_file('code_samples/catalog/custom_catalog_filter/src/CatalogFilter/DataTransformer/ProductNameCriterionTransformer.php') =]]
docs/product_catalog/create_custom_catalog_filter.md@52:```

001⫶<?php
002⫶
003⫶declare(strict_types=1);
004⫶
005⫶namespace App\CatalogFilter\DataTransformer;
006⫶
007⫶use Ibexa\Contracts\ProductCatalog\Values\Product\Query\Criterion\ProductName;
008⫶use Symfony\Component\Form\DataTransformerInterface;
009⫶use Symfony\Component\Form\Exception\TransformationFailedException;
010⫶
011⫶final class ProductNameCriterionTransformer implements DataTransformerInterface
012⫶{
013⫶ public function transform($value): ?string
014⫶ {
015⫶ if (null === $value) {
016⫶ return null;
017⫶ }
018⫶
019⫶ if (!$value instanceof ProductName) {
020⫶ throw new TransformationFailedException('Expected a ' . ProductName::class . ' object.');
021⫶ }
022⫶
023⫶ return $value->getName();
024⫶ }
025⫶
026⫶ public function reverseTransform($value): ?ProductName
027⫶ {
028⫶ if ($value === null) {
029⫶ return null;
030⫶ }
011⫶/**
012⫶ * @implements \Symfony\Component\Form\DataTransformerInterface<
013⫶ * \Ibexa\Contracts\ProductCatalog\Values\Product\Query\Criterion\ProductName,
014⫶ * \Ibexa\Bundle\ProductCatalog\Form\Data\Catalog\CatalogFilterPriceData
015⫶ * >
016⫶ */
017⫶final class ProductNameCriterionTransformer implements DataTransformerInterface
018⫶{
019⫶ public function transform($value): ?string
020⫶ {
021⫶ if (null === $value) {
022⫶ return null;
023⫶ }
024⫶
025⫶ if (!$value instanceof ProductName) {
026⫶ throw new TransformationFailedException('Expected a ' . ProductName::class . ' object.');
027⫶ }
028⫶
029⫶ return $value->getName();
030⫶ }
031⫶
031⫶
032⫶        if (!is_string($value)) {
033⫶ throw new TransformationFailedException('Invalid data, expected a string value');
034⫶ }
035⫶
036⫶ return new ProductName($value);
037⫶ }
038⫶}
032⫶    public function reverseTransform($value): ?ProductName
033⫶ {
034⫶ if ($value === null) {
035⫶ return null;
036⫶ }
037⫶
038⫶ if (!is_string($value)) {
039⫶ throw new TransformationFailedException('Invalid data, expected a string value');
040⫶ }
041⫶
042⫶ return new ProductName($value);
043⫶ }
044⫶}


Download colorized diff

@sonarqubecloud
Copy link
Copy Markdown

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