Skip to content

Commit b263076

Browse files
authored
Fix code quality issues in documentation code snippets (#8192)
- Remove return statement from void method in database-basics.md - Add missing parameter and return type declarations to Point class, PointType, validation methods, entity accessors/mutators, and error handler methods - Add proper exception throw for unhandled case in toExpression() - Replace empty($id) with !$id for defined parameter in form.md
1 parent 18e7ea4 commit b263076

File tree

5 files changed

+21
-21
lines changed

5 files changed

+21
-21
lines changed

docs/en/development/errors.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ use Cake\Event\EventInterface;
225225

226226
class ErrorController extends AppController
227227
{
228-
protected function missingWidget(MissingWidgetException $exception)
228+
protected function missingWidget(MissingWidgetException $exception): void
229229
{
230230
// You can prepare additional template context or trap errors.
231231
}
@@ -259,7 +259,7 @@ use Cake\Error\Renderer\WebExceptionRenderer;
259259

260260
class AppExceptionRenderer extends WebExceptionRenderer
261261
{
262-
public function missingWidget($error)
262+
public function missingWidget(\Throwable $error): \Cake\Http\Response
263263
{
264264
$response = $this->controller->getResponse();
265265

docs/en/orm/database-basics.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -749,8 +749,7 @@ class WidgetsTable extends Table
749749
{
750750
public function initialize(array $config): void
751751
{
752-
return parent::getSchema()->setColumnType('mutation', 'point_mutation');
753-
752+
parent::getSchema()->setColumnType('mutation', 'point_mutation');
754753
}
755754
}
756755
```
@@ -844,30 +843,30 @@ namespace App\Database;
844843
// Our value object is immutable.
845844
class Point
846845
{
847-
protected $_lat;
848-
protected $_long;
846+
protected float|int $_lat;
847+
protected float|int $_long;
849848

850849
// Factory method.
851-
public static function parse($value)
850+
public static function parse(mixed $value): static
852851
{
853852
// Parse the WKB data from MySQL.
854853
$unpacked = unpack('x4/corder/Ltype/dlat/dlong', $value);
855854

856855
return new static($unpacked['lat'], $unpacked['long']);
857856
}
858857

859-
public function __construct($lat, $long)
858+
public function __construct(float|int $lat, float|int $long)
860859
{
861860
$this->_lat = $lat;
862861
$this->_long = $long;
863862
}
864863

865-
public function lat()
864+
public function lat(): float|int
866865
{
867866
return $this->_lat;
868867
}
869868

870-
public function long()
869+
public function long(): float|int
871870
{
872871
return $this->_long;
873872
}
@@ -889,12 +888,12 @@ use Cake\Database\Type\ExpressionTypeInterface;
889888

890889
class PointType extends BaseType implements ExpressionTypeInterface
891890
{
892-
public function toPHP($value, Driver $d): mixed
891+
public function toPHP(mixed $value, Driver $driver): mixed
893892
{
894893
return $value === null ? null : Point::parse($value);
895894
}
896895

897-
public function marshal($value): mixed
896+
public function marshal(mixed $value): mixed
898897
{
899898
if (is_string($value)) {
900899
$value = explode(',', $value);
@@ -906,7 +905,7 @@ class PointType extends BaseType implements ExpressionTypeInterface
906905
return null;
907906
}
908907

909-
public function toExpression($value): ExpressionInterface
908+
public function toExpression(mixed $value): ExpressionInterface
910909
{
911910
if ($value instanceof Point) {
912911
return new FunctionExpression(
@@ -920,7 +919,8 @@ class PointType extends BaseType implements ExpressionTypeInterface
920919
if (is_array($value)) {
921920
return new FunctionExpression('POINT', [$value[0], $value[1]]);
922921
}
923-
// Handle other cases.
922+
923+
throw new \InvalidArgumentException('Cannot convert value to expression.');
924924
}
925925

926926
public function toDatabase($value, Driver $driver): mixed

docs/en/orm/entities.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ use Cake\ORM\Entity;
200200

201201
class Article extends Entity
202202
{
203-
protected function _getTitle($title)
203+
protected function _getTitle(string $title): string
204204
{
205205
return strtoupper($title);
206206
}
@@ -244,7 +244,7 @@ use Cake\Utility\Text;
244244

245245
class Article extends Entity
246246
{
247-
protected function _setTitle($title)
247+
protected function _setTitle(string $title): string
248248
{
249249
$this->slug = Text::slug($title);
250250

@@ -283,7 +283,7 @@ use Cake\ORM\Entity;
283283

284284
class User extends Entity
285285
{
286-
protected function _getFullName()
286+
protected function _getFullName(): string
287287
{
288288
return $this->first_name . ' ' . $this->last_name;
289289
}

docs/en/orm/validation.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ used. An example validator for our articles table would be:
113113
``` php
114114
class ArticlesTable extends Table
115115
{
116-
public function validationUpdate($validator)
116+
public function validationUpdate(Validator $validator): Validator
117117
{
118118
$validator
119119
->notEmptyString('title', __('You need to provide a title'))
@@ -682,7 +682,7 @@ In the above example, we'll use a 'custom' validator, which is defined using the
682682
`validationCustomName()` method:
683683

684684
``` php
685-
public function validationCustomName($validator)
685+
public function validationCustomName(Validator $validator): Validator
686686
{
687687
$validator->add(
688688
// ...
@@ -697,7 +697,7 @@ from any request:
697697

698698
``` php
699699
// In src/Model/Table/UsersTable.php
700-
public function validatePasswords($validator)
700+
public function validatePasswords(Validator $validator): Validator
701701
{
702702
$validator->add('confirm_password', 'no-misspelling', [
703703
'rule' => ['compareWith', 'password'],

docs/en/views/helpers/form.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ do the following:
8080
// src/Controller/ArticlesController.php:
8181
public function edit($id = null)
8282
{
83-
if (empty($id)) {
83+
if (!$id) {
8484
throw new NotFoundException;
8585
}
8686
$article = $this->Articles->get($id);

0 commit comments

Comments
 (0)