![]()
Nicolas Hart
Développeur Web chez TER'Informatique
@nclsHart
/**
* @param int $a
* @param int $b
*
* @return int
*/
function sum($a, $b)
{
return $a + $b;
}
function sum(int $a, int $b): int
{
return $a + $b;
}
function sum(int $a, int $b): int
{
return $a + $b;
}
var_dump(sum(5.8, 10.3)); // => int(15)
declare(strict_types=1);
function sum(int $a, int $b): int
{
return $a + $b;
}
var_dump(sum(5.8, 10.3));
// PHP Fatal error:
// Uncaught TypeError: Argument 1 passed to sum() must be of the type int,
// float given

function sum(int $a, int $b): int
{
return $a + $b;
}
sum(5.8, 10.3);
+------------------------------------------------------------------+
| Line | test.php |
+------------------------------------------------------------------+
| 8 | Parameter #1 $a of function sum expects int, float given. |
| 8 | Parameter #2 $b of function sum expects int, float given. |
+------------------------------------------------------------------+
[ERROR] Found 2 errors
/**
* @param array<string, bool> $options
*/
function configure(array $options): void
{
// ...
}
$options = [
'multiple' => false,
'required' => 1,
];
configure($options);
+--------------------------------------------------------------------+
| Line | test.php |
+--------------------------------------------------------------------+
| 16 | Parameter #1 $options of function configure expects |
| | array<string, bool>, array<string, int|false> given. |
+--------------------------------------------------------------------+
[ERROR] Found 1 error
class Employee
{
private $lastName;
public function getLastName(): ?string
{
return $this->lastName;
}
public function setLastName(string $lastName): void
{
$this->lastName = $lastName;
}
}
$employee = new Employee();
var_dump($employee->getLastName()); // => NULL
class Employee
{
private $lastName;
public function getLastName(): ?string
{
return $this->lastName;
}
public function setLastName(string $lastName): void
{
$this->lastName = $lastName;
}
}
$employee = new Employee();
var_dump($employee->getLastName()); // => NULL
class Employee
{
private $lastName;
public function __construct(string $lastName)
{
$this->lastName = $lastName;
}
public function getLastName(): string
{
return $this->lastName;
}
}
$employee = new Employee('Berriot');
var_dump($employee->getLastName()); // => string(7) "Berriot"
$order->setStatus(OrderStatus::CANCELLED);
$order->setCancelledAt(new \DateTimeImmutable());
$order->setReason('I do not need it anymore');
⇓
class Order
{
public function cancel(string $reason): void
{
$this->reason = $reason;
$this->status = OrderStatus::CANCELLED;
$this->cancelledAt = new \DateTimeImmutable();
}
}
class User
{
/**
* @var string
*/
private $email;
public function __construct(string $email)
{
$this->email = $email;
}
}
new User('tberriot'); // => no error
class User
{
public function __construct(Email $email)
{
$this->email = $email;
}
}
class Email
{
public function __construct(string $email)
{
Assert::email($email);
$this->email = $email;
}
}
new User(new Email('tberriot')); // => InvalidArgumentException
class OrderStatus
{
public const CANCELLED = 'cancelled';
public const DELIVERED = 'delivered';
}
switch ($status) {
case OrderStatus::CANCELLED:
// ...
break;
case OrderStatus::DELIVERED:
// ...
break;
default:
throw new \LogicException(
sprintf('Unexpected order status "%s".', $status)
);
}
public function awesomeFunction()
{
if (firstCondition) {
// do something
if (secondCondition) {
// do Something
}
}
}
public function awesomeFunction()
{
if (!firstCondition) {
return;
}
// do something
if (!secondCondition) {
return;
}
// do Something
}
public function awesomeFunction()
{
if (condition) {
// do something
} else {
// do something else
}
}
⇓
public function awesomeFunction()
{
if (condition) {
// do something
return;
}
// do something else
}
if ($value === 42) {
// ...
}
⇓
if (42 === $value) {
// ...
}
if ('cancelled' === $status) {
// ...
}
⇓
if (OrderStatus::CANCELLED === $status) {
// ...
}
public function send(Message $message, bool $async = false)
{
// ...
}
⇓
public function send(Message $message)
{
// ...
}
public function sendAsync(Message $message)
{
// ...
}
about 15 - 50 errors per 1000 lines of delivered code.