Code specification - Phan static analysis

Keywords: PHP Windows Mac Laravel

Phan

brief introduction

Phan is a PHP static analyzer that tends to minimize false positives. It tries to prove wrong, not right.

It looks for FAQs and verifies the type compatibility of various operations when type information is available or can be inferred.

Extended installation

  • ast

  • pcntl.so

    • mac: compiling and installing with source code
    • windows: not supported

Introduction

## install
composer require --dev phan/phan

## Initialize configuration
vendor/bin/phan --init --init-level=3

## implement
vendor/bin/phan

## Execute (windows)
sh vendor\bin\phan

Core configuration interpretation.phan/config.php

return [
    // The number of opened subprocesses needs to be expanded by pcntl, which is not supported by windows. After the mac is installed, it can be modified
    'processes' => 1,
    
    // Open progress bar
    'progress_bar' => true,
    
    // Resolved directory (including dependencies, which need to be exclude d)
    'directory_list' => [
        'app',
        'vendor' // It is recommended to minimize the loading of files
    ],
    
    // Directories that do not need to be resolved
    'exclude_analysis_directory_list' => [
        'vendor',
        '.phan',
    ],
    
    // Separate files to be resolved
    'file_list' => [],
    
    // List of files to be excluded
    'exclude_file_list' => [],
    
    // The auto loaded internal class library is generally used to load the extended stubs. The following introduces the stubs of laravel
    'autoload_internal_extension_signatures' => [
        'laravelIdeHelper' => '_ide_helper.php',
        'laravelMeta' => '.phpstorm.meta.php'
    ],
]

Ignore some errors : it is recommended not to use it as much as possible

suppress ignores the entire method

class D {
    /**
     * @suppress PhanUndeclaredClassMethod
     */
    function g() {
        C::f();
    }
}

@Phan suppress current line ignore current line

function test_line_suppression() {
    echo $undef1;  // @phan-suppress-current-line PhanUndeclaredVariable
    echo $undef2 + missingFn();
}

@Phan suppress next line ignore next line

function test_line_suppression() {
    // @phan-suppress-next-line PhanUndeclaredVariable, PhanUndeclaredFunction
    echo $undefVar2 + missing_function();  
}

Posted by chelerblondi on Sat, 16 Nov 2019 12:23:49 -0800