Skip to content

Commit 47bf29d

Browse files
authored
Add missing types to code examples (#8213)
* Add missing param types, return types, and property types to code examples. Also fix loadHelper() to addHelper() in View initialize() contexts. * Add missing property types to fixture and test class examples. * Fix up code snippets.
1 parent 03029bf commit 47bf29d

33 files changed

Lines changed: 103 additions & 103 deletions

docs/en/appendices/5-0-migration-guide.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ changes made:
113113
### Database
114114

115115
- The `DateTimeType` and `DateType` now always return immutable objects.
116-
Additionally the interface for `Date` objects reflects the `ChronosDate`
116+
Additionally, the interface for `Date` objects reflects the `ChronosDate`
117117
interface which lacks all the time related methods that were present in
118118
CakePHP 4.x.
119119
- `DateType::setLocaleFormat()` no longer accepts an array.
@@ -364,7 +364,7 @@ Table finders can now have typed arguments as required instead of an options arr
364364
For e.g. a finder for fetching posts by category or user:
365365

366366
``` php
367-
public function findByCategoryOrUser(SelectQuery $query, array $options)
367+
public function findByCategoryOrUser(SelectQuery $query, array $options): SelectQuery
368368
{
369369
if (isset($options['categoryId'])) {
370370
$query->where(['category_id' => $options['categoryId']]);
@@ -380,7 +380,7 @@ public function findByCategoryOrUser(SelectQuery $query, array $options)
380380
can now be written as:
381381

382382
``` php
383-
public function findByCategoryOrUser(SelectQuery $query, ?int $categoryId = null, ?int $userId = null)
383+
public function findByCategoryOrUser(SelectQuery $query, ?int $categoryId = null, ?int $userId = null): SelectQuery
384384
{
385385
if ($categoryId) {
386386
$query->where(['category_id' => $categoryId]);

docs/en/console-commands/commands.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ use Cake\Console\ConsoleOptionParser;
170170
class UserCommand extends Command
171171
{
172172
// Define the default table. This allows you to use `fetchTable()` without any argument.
173-
protected $defaultTable = 'Users';
173+
protected ?string $defaultTable = 'Users';
174174

175175
protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
176176
{
@@ -207,7 +207,7 @@ to terminate execution:
207207
public function execute(Arguments $args, ConsoleIo $io): int
208208
{
209209
$name = $args->getArgument('name');
210-
if (strlen($name) < 5) {
210+
if (mb_strlen($name) < 5) {
211211
// Halt execution, output to stderr, and set exit code to 1
212212
$io->error('Name must be at least 4 characters long.');
213213
$this->abort();
@@ -223,7 +223,7 @@ You can also use `abort()` on the `$io` object to emit a message and code:
223223
public function execute(Arguments $args, ConsoleIo $io): int
224224
{
225225
$name = $args->getArgument('name');
226-
if (strlen($name) < 5) {
226+
if (mb_strlen($name) < 5) {
227227
// Halt execution, output to stderr, and set exit code to 99
228228
$io->abort('Name must be at least 4 characters long.', 99);
229229
}
@@ -485,7 +485,7 @@ class UpdateTableCommandTest extends TestCase
485485
{
486486
use ConsoleIntegrationTestTrait;
487487

488-
protected $fixtures = [
488+
protected array $fixtures = [
489489
// assumes you have a UsersFixture
490490
'app.Users',
491491
];

docs/en/console-commands/option-parsers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ can be used as part of a fluent method chain.
9292

9393
When creating positional arguments, you can use the `required` flag, to
9494
indicate that an argument must be present when a shell is called.
95-
Additionally you can use `choices` to force an argument to be from a list of
95+
Additionally, you can use `choices` to force an argument to be from a list of
9696
valid choices:
9797

9898
``` php

docs/en/contributing/cakephp-coding-conventions.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ As you can see above there should be one space on both sides of equals sign (=).
224224
Example of a method definition:
225225

226226
``` php
227-
public function someFunction($arg1, $arg2 = '')
227+
public function someFunction(string $arg1, string $arg2 = ''): mixed
228228
{
229229
if (expr) {
230230
statement;
@@ -239,7 +239,7 @@ Try to make your functions return something, at least `true` or `false`, so
239239
it can be determined whether the function call was successful:
240240

241241
``` php
242-
public function connection($dns, $persistent = false)
242+
public function connection(string|array $dns, bool $persistent = false): bool
243243
{
244244
if (is_array($dns)) {
245245
$dnsInfo = $dns;
@@ -262,7 +262,7 @@ There are spaces on both side of the equals sign.
262262
Try to avoid unnecessary nesting by bailing early:
263263

264264
``` php
265-
public function run(array $data)
265+
public function run(array $data): bool
266266
{
267267
...
268268
if (!$success) {
@@ -272,7 +272,7 @@ public function run(array $data)
272272
...
273273
}
274274

275-
public function check(array $data)
275+
public function check(array $data): void
276276
{
277277
...
278278
if (!$success) {
@@ -299,7 +299,7 @@ We only typehint public methods, though, as typehinting is not cost-free:
299299
* @param callable $callback Some callback.
300300
* @param bool $boolean Some boolean value.
301301
*/
302-
public function foo(Table $table, array $array, callable $callback, $boolean)
302+
public function foo(Table $table, array $array, callable $callback, bool $boolean): void
303303
{
304304
}
305305
```
@@ -317,7 +317,7 @@ type:
317317
*
318318
* @param array|\ArrayObject $array Some array value.
319319
*/
320-
public function foo($array)
320+
public function foo(array|\ArrayObject $array): void
321321
{
322322
}
323323
```
@@ -456,7 +456,7 @@ instead:
456456
*
457457
* @return $this
458458
*/
459-
public function foo()
459+
public function foo(): static
460460
{
461461
return $this;
462462
}
@@ -650,7 +650,7 @@ class Thing
650650
{
651651
private $property; // Defined
652652

653-
public function readProperty()
653+
public function readProperty(): void
654654
{
655655
// Not recommended as the property is defined in the class
656656
if (!isset($this->property)) {

docs/en/core-libraries/caching.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ For example, you often want to cache remote service call results. You could use
361361
``` php
362362
class IssueService
363363
{
364-
public function allIssues($repo)
364+
public function allIssues(string $repo): mixed
365365
{
366366
return Cache::remember($repo . '-issues', function () use ($repo) {
367367
return $this->fetchAll($repo);
@@ -576,7 +576,7 @@ remove all entries associated to the `article` group:
576576

577577
``` php
578578
// src/Model/Table/ArticlesTable.php
579-
public function afterSave($event, $entity, $options = [])
579+
public function afterSave(EventInterface $event, EntityInterface $entity, ArrayObject $options): void
580580
{
581581
if ($entity->isNew()) {
582582
Cache::clearGroup('article', 'site_home');
@@ -598,7 +598,7 @@ configurations, i.e.: having the same group:
598598
* A variation of previous example that clears all Cache configurations
599599
* having the same group
600600
*/
601-
public function afterSave($event, $entity, $options = [])
601+
public function afterSave(EventInterface $event, EntityInterface $entity, ArrayObject $options): void
602602
{
603603
if ($entity->isNew()) {
604604
$configs = Cache::groupConfigs('article');

docs/en/core-libraries/collections.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,7 +1265,7 @@ This can be refactored by creating another class:
12651265
``` php
12661266
class TotalOrderCalculator
12671267
{
1268-
public function __invoke($row, $key)
1268+
public function __invoke(array $row, string $key): array
12691269
{
12701270
if (!empty($row['items'])) {
12711271
$row['total'] = collection($row['items'])->sumOf('price');
@@ -1309,7 +1309,7 @@ be repeated every time:
13091309
``` php
13101310
class FinalCheckOutRowProcessor
13111311
{
1312-
public function __invoke($collection)
1312+
public function __invoke(CollectionInterface $collection): CollectionInterface
13131313
{
13141314
return $collection
13151315
->map(new ShippingCostCalculator)
@@ -1396,7 +1396,7 @@ The `buffered()` method is also useful for converting non-rewindable iterators
13961396
into collections that can be iterated more than once:
13971397

13981398
``` php
1399-
public function results()
1399+
public function results(): Generator
14001400
{
14011401
...
14021402
foreach ($transientElements as $e) {

docs/en/core-libraries/email.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ use Cake\Mailer\Mailer;
358358

359359
class UserMailer extends Mailer
360360
{
361-
public function welcome($user)
361+
public function welcome(User $user): void
362362
{
363363
$this
364364
->setTo($user->email)
@@ -367,7 +367,7 @@ class UserMailer extends Mailer
367367
->setTemplate('welcome_mail'); // By default, template with same name as method name is used.
368368
}
369369

370-
public function resetPassword($user)
370+
public function resetPassword(User $user): void
371371
{
372372
$this
373373
->setTo($user->email)

docs/en/core-libraries/events.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ use Cake\ORM\Table;
5656

5757
class OrdersTable extends Table
5858
{
59-
public function place($order)
59+
public function place(Order $order): bool
6060
{
6161
if ($this->save($order)) {
6262
$this->Cart->remove($order);
@@ -536,7 +536,7 @@ In order to stop events you can either return `false` in your callbacks or
536536
call the `stopPropagation()` method on the event object:
537537

538538
``` php
539-
public function doSomething(EventInterface $event)
539+
public function doSomething(EventInterface $event): bool
540540
{
541541
// ...
542542
return false; // Stops the event
@@ -550,7 +550,7 @@ public function updateBuyStatistic(EventInterface $event): void
550550
```
551551

552552
Stopping an event will prevent any additional callbacks from being called.
553-
Additionally the code triggering the event may behave differently based on the
553+
Additionally, the code triggering the event may behave differently based on the
554554
event being stopped or not. Generally it does not make sense to stop 'after'
555555
events, but stopping 'before' events is often used to prevent the entire
556556
operation from occurring.
@@ -559,7 +559,7 @@ To check if an event was stopped, you call the `isStopped()` method in the
559559
event object:
560560

561561
``` php
562-
public function place($order)
562+
public function place(Order $order): bool
563563
{
564564
$event = new Event('Order.beforePlace', $this, ['order' => $order]);
565565
$this->getEventManager()->dispatch($event);
@@ -588,7 +588,7 @@ directly or returning the value in the callback itself:
588588

589589
``` php
590590
// A listener callback
591-
public function doSomething(EventInterface $event)
591+
public function doSomething(EventInterface $event): mixed
592592
{
593593
// ...
594594
$alteredData = $event->getData('order') + $moreData;
@@ -604,7 +604,7 @@ public function doSomethingElse(EventInterface $event): void
604604
}
605605

606606
// Using the event result
607-
public function place($order)
607+
public function place(Order $order): bool
608608
{
609609
$event = new Event('Order.beforePlace', $this, ['order' => $order]);
610610
$this->getEventManager()->dispatch($event);

docs/en/core-libraries/form.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ invalidate the fields accordingly to the feedback from the remote server:
200200

201201
``` php
202202
// in src/Form/ContactForm.php
203-
public function setErrors($errors)
203+
public function setErrors(array $errors): void
204204
{
205205
$this->_errors = $errors;
206206
}

docs/en/core-libraries/hash.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ this method:
666666
// Call the noop function $this->noop() on every element of $data
667667
$result = Hash::map($data, "{n}", [$this, 'noop']);
668668

669-
public function noop(array $array)
669+
public function noop(array $array): array
670670
{
671671
// Do stuff to array and return the result
672672
return $array;

0 commit comments

Comments
 (0)