Skip to content

Commit 0cc3474

Browse files
authored
Merge pull request #1103 from cakephp/docblock
Use "array<>" form instead of "[]" to denote arrays in docblocks
2 parents 8d6ca5b + c5efb37 commit 0cc3474

37 files changed

Lines changed: 69 additions & 82 deletions

‎src/View/Helper/DocBlockHelper.php‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ class DocBlockHelper extends Helper
2121
/**
2222
* @var bool Whether to add a blank line between different class annotations
2323
*/
24-
protected bool $annotationSpacing = true;
24+
protected bool $annotationSpacing = false;
2525

2626
/**
2727
* Writes the DocBlock header for a class which includes the property and method declarations. Annotations are
28-
* sorted and grouped by type and value. Groups of annotations are separated by blank lines.
28+
* sorted and grouped by type and value. If enabled via the `$annotationSpacing` setting of this helper, groups of
29+
* annotations are separated by blank lines.
2930
*
3031
* @param string $className The class this comment block is for.
3132
* @param string $classType The type of class (example, Entity)
@@ -80,7 +81,7 @@ public function associatedEntityTypeToHintType(string $type, Association $associ
8081
$annotationType === Association::MANY_TO_MANY ||
8182
$annotationType === Association::ONE_TO_MANY
8283
) {
83-
return $type . '[]';
84+
return 'array<' . $type . '>';
8485
}
8586

8687
return $type;

‎templates/bake/Model/entity.twig‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
{% set annotations = DocBlock.propertyHints(propertyHintMap) %}
1919

2020
{%- if associationHintMap %}
21-
{%~ set annotations = annotations|merge(['']) %}
2221
{%~ set annotations = annotations|merge(DocBlock.propertyHints(associationHintMap)) %}
2322
{% endif %}
2423

‎templates/bake/Template/add.twig‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
* @var \{{ entityClass }} ${{ singularVar }}
2020
{% if associations.BelongsTo is defined %}
2121
{%~ for assocName, assocData in associations.BelongsTo %}
22-
* @var \Cake\Collection\CollectionInterface|string[] ${{ assocData.variable }}
22+
* @var \Cake\Collection\CollectionInterface|array<string> ${{ assocData.variable }}
2323
{%~ endfor %}
2424
{% endif %}
2525
{% if associations.BelongsToMany is defined %}
2626
{%~ for assocName, assocData in associations.BelongsToMany %}
27-
* @var \Cake\Collection\CollectionInterface|string[] ${{ assocData.variable }}
27+
* @var \Cake\Collection\CollectionInterface|array<string> ${{ assocData.variable }}
2828
{%~ endfor %}
2929
{% endif %}
3030
*/

‎templates/bake/Template/edit.twig‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
* @var \{{ entityClass }} ${{ singularVar }}
2020
{% if associations.BelongsTo is defined %}
2121
{%~ for assocName, assocData in associations.BelongsTo %}
22-
* @var string[]|\Cake\Collection\CollectionInterface ${{ assocData.variable }}
22+
* @var array<string>|\Cake\Collection\CollectionInterface ${{ assocData.variable }}
2323
{%~ endfor %}
2424
{% endif %}
2525
{% if associations.BelongsToMany is defined %}
2626
{%~ for assocName, assocData in associations.BelongsToMany %}
27-
* @var string[]|\Cake\Collection\CollectionInterface ${{ assocData.variable }}
27+
* @var array<string>|\Cake\Collection\CollectionInterface ${{ assocData.variable }}
2828
{%~ endfor %}
2929
{% endif %}
3030
*/

‎tests/TestCase/Command/PluginCommandTest.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ public function assertPluginContents($pluginName, bool $vendor = false): void
281281
/**
282282
* Get recursive files list for given path.
283283
*
284-
* @return string[]
284+
* @return array<string>
285285
*/
286286
protected function getFiles(string $path): array
287287
{

‎tests/TestCase/Command/SimpleBakeCommandTest.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public function testBakePlugin(): void
129129
/**
130130
* Provider for subclasses.
131131
*
132-
* @return string[][]
132+
* @return array<array<string>>
133133
*/
134134
public static function subclassProvider(): array
135135
{

‎tests/TestCase/View/Helper/DocBlockHelperTest.php‎

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Cake\ORM\Table;
2929
use Cake\TestSuite\TestCase;
3030
use PHPUnit\Framework\Attributes\CoversClass;
31+
use ReflectionProperty;
3132

3233
/**
3334
* DocBlockHelper Test
@@ -72,7 +73,7 @@ protected function tearDown(): void
7273
}
7374

7475
/**
75-
* Tests the classDescription method including annotation spacing
76+
* Tests the classDescription method
7677
*
7778
* @return void
7879
*/
@@ -85,6 +86,28 @@ public function testClassDescription(): void
8586
'@foo $bar baz',
8687
'@see there',
8788
];
89+
$classDescription = $this->DocBlockHelper->classDescription($className, $classType, $lines);
90+
$expected = "/**\n * Comments Model\n *\n * Line 1\n * @foo \$bar baz\n * @see there\n */";
91+
$this::assertSame($expected, $classDescription);
92+
}
93+
94+
/**
95+
* Tests the classDescription method with annotation spacing enabled
96+
*
97+
* @return void
98+
*/
99+
public function testClassDescriptionAnnotationSpacing(): void
100+
{
101+
$className = 'Comments';
102+
$classType = 'Model';
103+
$lines = [
104+
'Line 1',
105+
'@foo $bar baz',
106+
'@see there',
107+
];
108+
$reflection = new ReflectionProperty($this->DocBlockHelper, 'annotationSpacing');
109+
$reflection->setValue($this->DocBlockHelper, true);
110+
88111
$classDescription = $this->DocBlockHelper->classDescription($className, $classType, $lines);
89112
$expected = "/**\n * Comments Model\n *\n * Line 1\n * @foo \$bar baz\n *\n * @see there\n */";
90113
$this::assertSame($expected, $classDescription);
@@ -103,14 +126,14 @@ public function testAssociatedEntityTypeToHintType(): void
103126
$type = 'Foo';
104127
$association = new BelongsToMany('Foo', $sourceTable);
105128
$assocEntityType = $this->DocBlockHelper->associatedEntityTypeToHintType($type, $association);
106-
$expected = 'Foo[]';
129+
$expected = 'array<Foo>';
107130
$this->assertSame($expected, $assocEntityType);
108131

109132
// Test with ONE_TO_MANY
110133
$type = 'Bar';
111134
$association = new HasMany('Bar', $sourceTable);
112135
$assocEntityType = $this->DocBlockHelper->associatedEntityTypeToHintType($type, $association);
113-
$expected = 'Bar[]';
136+
$expected = 'array<Bar>';
114137
$this->assertSame($expected, $assocEntityType);
115138

116139
// Test with ONE_TO_ONE

‎tests/comparisons/Model/testBakeAssociationDetectionCategoriesProductsTable.php‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
*
1414
* @property \Bake\Test\App\Model\Table\CategoriesTable&\Cake\ORM\Association\BelongsTo $Categories
1515
* @property \Bake\Test\App\Model\Table\ProductsTable&\Cake\ORM\Association\BelongsTo $Products
16-
*
1716
* @method \Bake\Test\App\Model\Entity\CategoriesProduct newEmptyEntity()
1817
* @method \Bake\Test\App\Model\Entity\CategoriesProduct newEntity(array $data, array $options = [])
1918
* @method array<\Bake\Test\App\Model\Entity\CategoriesProduct> newEntities(array $data, array $options = [])

‎tests/comparisons/Model/testBakeAssociationDetectionCategoriesTable.php‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
* Categories Model
1313
*
1414
* @property \Bake\Test\App\Model\Table\ProductsTable&\Cake\ORM\Association\BelongsToMany $Products
15-
*
1615
* @method \Bake\Test\App\Model\Entity\Category newEmptyEntity()
1716
* @method \Bake\Test\App\Model\Entity\Category newEntity(array $data, array $options = [])
1817
* @method array<\Bake\Test\App\Model\Entity\Category> newEntities(array $data, array $options = [])
@@ -26,7 +25,6 @@
2625
* @method iterable<\Bake\Test\App\Model\Entity\Category>|\Cake\Datasource\ResultSetInterface<\Bake\Test\App\Model\Entity\Category> saveManyOrFail(iterable $entities, array $options = [])
2726
* @method iterable<\Bake\Test\App\Model\Entity\Category>|\Cake\Datasource\ResultSetInterface<\Bake\Test\App\Model\Entity\Category>|false deleteMany(iterable $entities, array $options = [])
2827
* @method iterable<\Bake\Test\App\Model\Entity\Category>|\Cake\Datasource\ResultSetInterface<\Bake\Test\App\Model\Entity\Category> deleteManyOrFail(iterable $entities, array $options = [])
29-
*
3028
* @mixin \Cake\ORM\Behavior\TimestampBehavior
3129
*/
3230
class CategoriesTable extends Table

‎tests/comparisons/Model/testBakeAssociationDetectionCategoriesTableSigned.php‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
* Categories Model
1313
*
1414
* @property \Bake\Test\App\Model\Table\ProductsTable&\Cake\ORM\Association\BelongsToMany $Products
15-
*
1615
* @method \Bake\Test\App\Model\Entity\Category newEmptyEntity()
1716
* @method \Bake\Test\App\Model\Entity\Category newEntity(array $data, array $options = [])
1817
* @method array<\Bake\Test\App\Model\Entity\Category> newEntities(array $data, array $options = [])
@@ -26,7 +25,6 @@
2625
* @method iterable<\Bake\Test\App\Model\Entity\Category>|\Cake\Datasource\ResultSetInterface<\Bake\Test\App\Model\Entity\Category> saveManyOrFail(iterable $entities, array $options = [])
2726
* @method iterable<\Bake\Test\App\Model\Entity\Category>|\Cake\Datasource\ResultSetInterface<\Bake\Test\App\Model\Entity\Category>|false deleteMany(iterable $entities, array $options = [])
2827
* @method iterable<\Bake\Test\App\Model\Entity\Category>|\Cake\Datasource\ResultSetInterface<\Bake\Test\App\Model\Entity\Category> deleteManyOrFail(iterable $entities, array $options = [])
29-
*
3028
* @mixin \Cake\ORM\Behavior\TimestampBehavior
3129
*/
3230
class CategoriesTable extends Table

0 commit comments

Comments
 (0)