Search code examples
phpphpstan

Why PHPStan can't find the class installation by pear?


PHPStan can't find the classes I installed with pear.
Why? How can I fix it?

Steps

  1. Run: pear install HTTP_Request2-2.5.1
  2. Check: pear config-get php_dir
/usr/share/pear
  1. Check: php -i | grep include_path
include_path => .:/usr/share/pear:/usr/share/php => .:/usr/share/pear:/usr/share/php
  1. Check: ls -l /usr/share/pear/HTTP/Request2.php
-rw-r--r--. 1 root root 36102  Aug 19 11:38 /usr/share/pear/HTTP/Request2.php
  1. Write test.php
<?php

require_once 'HTTP/Request2.php';

$request = new \HTTP_Request2("http://example.com");
  1. Run phpstan analyze test.php
 ------ --------------------------------------------------------------------- 
  Line   test.php                                                             
 ------ --------------------------------------------------------------------- 
  5      Instantiated class HTTP_Request2 not found.                          
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols   
 ------ --------------------------------------------------------------------- 

Solution

  • You have to specify the PEAR library path using phpstan.neon config file.

    parameters:
        scanDirectories:
            - /usr/share/pear
        level: 5
    
    $ phpstan analyze -c path/to/phpstan.neon test.php
    

    Since your PEAR library HTTP_Request2 has not been installed via composer, PHPStan doesn't aware where it's from.

    It seems PHPStan doesn't care include_path intentionally.