Skip to content

compatibility

Ensure that implemented features are compatible with the set version of PHP

Options

version

The version of PHP to check for compatibility

Type: string

Default: 8.0

Possible values: 8.0, 8.1, 8.2

Examples

  • Examples of correct code for this rule using default options

All statements are compatible

php
<?php
class BlogData
{
    private Status $status;

    public function __construct(Status $status)
    {
        $this->status = $status;
    }

    public function getStatus(): Status
    {
        return $this->status;
    }
}

function count_and_iterate(Iterator $value)
{
    if (! ($value instanceof Countable)) {
        throw new TypeError('value must be Countable');
    }

    foreach ($value as $val) {
        echo $val;
    }

    count($value);
}

function redirect(string $uri)
{
    header('Location: ' . $uri);
    exit();
}

function redirect_to_login_page()
{
    redirect('/login');
    echo 'Hello'; // <- dead code
}

class Foo
{
    public const XX = "foo";
}

class Bar extends Foo
{
    public const XX = "bar"; // No error
}

$array_a = array('a' => 1);
$array_b = array('b' => 2);

$result = array_merge(array('a' => 0), $array_a, $array_b);
  • Examples of incorrect code for this rule using default options

Incompatible statements

php
<?php

$array_a = array('a' => 1);
$array_b = array('b' => 2);
$result = array('a' => 0, ...$array_a, ...$array_b);

function redirect(string $uri): never
{
    header('Location: ' . $uri);
    exit();
}

function redirect_to_login_page(): never
{
    redirect('/login');
    echo 'Hello'; // <- dead code detected by static analysis
}

function count_and_iterate(Iterator&Countable $value, string $test)
{
    foreach ($value as $val) {
        echo $val;
    }

    count($value);
}

function function_with_return_type($value, string $test): FirstType&SecondType
{
    foreach ($value as $val) {
        echo $val;
    }

    count($value);
}

readonly class BlogData
{
    public readonly Status $status;

   
    public function alwaysFalse(false $variable): false
    {
        echo "Function alwaysFalse() is called";
    }

    public function alwaysTrue(true $trueVar): true
    {
        echo "Function alwaysTrue() is called";
    }

    public function alwaysNull(null $nullVar): null
    {
        echo "Function alwaysNull() is called";
    }

    public function __construct(Status $status)
    {
        $this->status = $status;
    }

    private function countAndIterate($value, string $test): Iterator&Countable
    {
        foreach ($value as $val) {
            echo $val;
        }

        count($value);
    }
}

enum Status
{
    case Draft;
    case Published;
    case Archived;
}

version

  • Examples of correct code for this rule using version option

Incompatible statements with PHP 8.1

php
<?php
/* taqwim "taqwim/compatibility": {version: "8.1"} */
$array_a = array('a' => 1);
$array_b = array('b' => 2);
$result = array('a' => 0, ...$array_a, ...$array_b);

function redirect(string $uri): never
{
    header('Location: ' . $uri);
    exit();
}

function redirect_to_login_page(): never
{
    redirect('/login');
    echo 'Hello'; // <- dead code detected by static analysis
}

function count_and_iterate(Iterator&Countable $value, string $test)
{
    foreach ($value as $val) {
        echo $val;
    }

    count($value);
}

function function_with_return_type($value, string $test): FirstType&SecondType
{
    foreach ($value as $val) {
        echo $val;
    }

    count($value);
}

class BlogData
{
    public readonly Status $status;

    public function __construct(Status $status)
    {
        $this->status = $status;
    }

    private function countAndIterate($value, string $test): Iterator&Countable
    {
        foreach ($value as $val) {
            echo $val;
        }

        count($value);
    }
}

enum Status
{
    case Draft;
    case Published;
    case Archived;
}

Incompatible statements with PHP 8.2

php
<?php
/* taqwim "taqwim/compatibility": {version: "8.2"} */
$array_a = array('a' => 1);
$array_b = array('b' => 2);
$result = array('a' => 0, ...$array_a, ...$array_b);

function redirect(string $uri): never
{
    header('Location: ' . $uri);
    exit();
}

function redirect_to_login_page(): never
{
    redirect('/login');
    echo 'Hello'; // <- dead code detected by static analysis
}

function count_and_iterate(Iterator&Countable $value, string $test)
{
    foreach ($value as $val) {
        echo $val;
    }

    count($value);
}

function function_with_return_type($value, string $test): FirstType&SecondType
{
    foreach ($value as $val) {
        echo $val;
    }

    count($value);
}

readonly class BlogData
{
    public readonly Status $status;

    public function alwaysFalse(false $variable): false
    {
        echo "Function alwaysFalse() is called";
    }

    public function alwaysTrue(true $trueVar): true
    {
        echo "Function alwaysTrue() is called";
    }

    public function alwaysNull(null $nullVar): null
    {
        echo "Function alwaysNull() is called";
    }

    public function __construct(Status $status)
    {
        $this->status = $status;
    }

    private function countAndIterate($value, string $test): Iterator&Countable
    {
        foreach ($value as $val) {
            echo $val;
        }

        count($value);
    }
}

enum Status
{
    case Draft;
    case Published;
    case Archived;
}

Released under the MIT License.