PHP code example of techno-artisan / phpstan-strict-rules

1. Go to this page and download the library: Download techno-artisan/phpstan-strict-rules library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

techno-artisan / phpstan-strict-rules example snippets


class Config { const TIMEOUT = 30; }     // ❌ technoArtisan.typedClassConstant
if (empty($user)) { /* … */ }            // ❌ technoArtisan.disallowedEmpty
if ($id == $request->id) { /* … */ }     // ❌ technoArtisan.looseComparison
in_array('1', [1, 2, 3]);                // ❌ technoArtisan.looseInArray  (returns true!)

// ❌ reported
final class Config
{
    const TIMEOUT = 30;
}

// ✅ instead
final class Config
{
    const int TIMEOUT = 30;
}

// ❌ reported
if (empty($value)) {}

// ✅ instead
if ($value === null || $value === '') {}

// ❌ reported
in_array('1', [1, 2, 3]);          // true — '1' is loosely equal to 1

// ✅ instead
in_array('1', [1, 2, 3], true);    // false

// ❌ reported
if ($id == $input) {}   // 0 == 'foo', '1e1' == '10' and null == false are all true

// ✅ instead
if ($id === $input) {}

$x == $y; // @phpstan-ignore technoArtisan.looseComparison