2016-06-06 07:07:50 +02:00
|
|
|
<?php
|
2016-07-26 00:37:44 +02:00
|
|
|
namespace Psalm;
|
2016-06-06 07:07:50 +02:00
|
|
|
|
2018-03-03 21:19:05 +01:00
|
|
|
use Composer\Autoload\ClassLoader;
|
2017-01-02 21:31:18 +01:00
|
|
|
use Psalm\Checker\ClassLikeChecker;
|
|
|
|
use Psalm\Checker\ProjectChecker;
|
2016-12-30 02:07:42 +01:00
|
|
|
use Psalm\Config\IssueHandler;
|
|
|
|
use Psalm\Config\ProjectFileFilter;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\Exception\ConfigException;
|
2018-01-21 18:44:46 +01:00
|
|
|
use Psalm\Scanner\FileScanner;
|
2016-06-10 00:08:25 +02:00
|
|
|
use SimpleXMLElement;
|
|
|
|
|
2016-06-06 07:07:50 +02:00
|
|
|
class Config
|
|
|
|
{
|
2016-07-26 00:39:36 +02:00
|
|
|
const DEFAULT_FILE_NAME = 'psalm.xml';
|
2016-06-25 00:18:11 +02:00
|
|
|
const REPORT_INFO = 'info';
|
|
|
|
const REPORT_ERROR = 'error';
|
2016-06-26 19:45:20 +02:00
|
|
|
const REPORT_SUPPRESS = 'suppress';
|
2016-06-25 00:18:11 +02:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @var array<string>
|
|
|
|
*/
|
2016-06-27 21:10:13 +02:00
|
|
|
public static $ERROR_LEVELS = [
|
2016-06-25 00:18:11 +02:00
|
|
|
self::REPORT_INFO,
|
|
|
|
self::REPORT_ERROR,
|
2017-05-27 02:05:57 +02:00
|
|
|
self::REPORT_SUPPRESS,
|
2016-06-25 00:18:11 +02:00
|
|
|
];
|
|
|
|
|
2016-12-19 01:17:39 +01:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected static $MIXED_ISSUES = [
|
|
|
|
'MixedArgument',
|
|
|
|
'MixedArrayAccess',
|
2017-11-19 19:42:48 +01:00
|
|
|
'MixedArrayAssignment',
|
2016-12-19 01:17:39 +01:00
|
|
|
'MixedArrayOffset',
|
|
|
|
'MixedAssignment',
|
|
|
|
'MixedInferredReturnType',
|
|
|
|
'MixedMethodCall',
|
2016-12-24 03:30:32 +01:00
|
|
|
'MixedOperand',
|
2016-12-19 01:17:39 +01:00
|
|
|
'MixedPropertyFetch',
|
|
|
|
'MixedPropertyAssignment',
|
2018-01-05 03:36:16 +01:00
|
|
|
'MixedReturnStatement',
|
2017-05-27 02:05:57 +02:00
|
|
|
'MixedStringOffsetAssignment',
|
2017-11-19 19:42:48 +01:00
|
|
|
'MixedTypeCoercion',
|
2016-12-19 01:17:39 +01:00
|
|
|
];
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @var self|null
|
|
|
|
*/
|
2018-01-21 19:38:51 +01:00
|
|
|
private static $instance;
|
2016-06-10 00:08:25 +02:00
|
|
|
|
2016-06-27 04:40:57 +02:00
|
|
|
/**
|
|
|
|
* Whether or not to use types as defined in docblocks
|
2016-11-01 05:39:41 +01:00
|
|
|
*
|
2017-05-27 02:16:18 +02:00
|
|
|
* @var bool
|
2016-06-27 04:40:57 +02:00
|
|
|
*/
|
2016-08-24 06:22:38 +02:00
|
|
|
public $use_docblock_types = true;
|
2016-06-10 00:08:25 +02:00
|
|
|
|
2018-01-13 06:32:20 +01:00
|
|
|
/**
|
|
|
|
* Whether or not to use types as defined in property docblocks.
|
|
|
|
* This is distinct from the above because you may want to use
|
|
|
|
* property docblocks, but not function docblocks.
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $use_docblock_property_types = true;
|
|
|
|
|
2016-06-27 04:40:57 +02:00
|
|
|
/**
|
|
|
|
* Whether or not to throw an exception on first error
|
2016-11-01 05:39:41 +01:00
|
|
|
*
|
2017-05-27 02:16:18 +02:00
|
|
|
* @var bool
|
2016-06-27 04:40:57 +02:00
|
|
|
*/
|
|
|
|
public $throw_exception = false;
|
|
|
|
|
2016-07-26 21:00:40 +02:00
|
|
|
/**
|
2016-11-04 22:45:12 +01:00
|
|
|
* The directory to store PHP Parser (and other) caches
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2017-01-15 23:20:54 +01:00
|
|
|
public $cache_directory;
|
2016-11-04 22:45:12 +01:00
|
|
|
|
2017-02-27 18:02:13 +01:00
|
|
|
/**
|
|
|
|
* Whether or not to care about casing of file names
|
|
|
|
*
|
2017-05-27 02:16:18 +02:00
|
|
|
* @var bool
|
2017-02-27 18:02:13 +01:00
|
|
|
*/
|
|
|
|
public $use_case_sensitive_file_names = false;
|
|
|
|
|
2016-11-04 22:45:12 +01:00
|
|
|
/**
|
|
|
|
* Path to the autoader
|
2016-11-01 05:39:41 +01:00
|
|
|
*
|
2016-07-26 21:00:40 +02:00
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
public $autoloader;
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
2016-12-30 02:07:42 +01:00
|
|
|
* @var ProjectFileFilter|null
|
2016-11-02 07:29:00 +01:00
|
|
|
*/
|
2017-07-25 23:04:58 +02:00
|
|
|
protected $project_files;
|
2016-06-10 00:08:25 +02:00
|
|
|
|
2016-09-01 05:12:35 +02:00
|
|
|
/**
|
|
|
|
* The base directory of this config file
|
2016-11-01 05:39:41 +01:00
|
|
|
*
|
2016-09-01 05:12:35 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
2017-07-25 23:04:58 +02:00
|
|
|
protected $base_dir;
|
2016-06-06 07:07:50 +02:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @var array<int, string>
|
|
|
|
*/
|
2017-02-12 00:56:38 +01:00
|
|
|
private $file_extensions = ['php'];
|
2016-06-10 20:47:44 +02:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
2017-02-24 01:36:51 +01:00
|
|
|
* @var array<string, string>
|
2016-11-02 07:29:00 +01:00
|
|
|
*/
|
2018-01-21 19:38:51 +01:00
|
|
|
private $filetype_scanners = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array<string, string>
|
|
|
|
*/
|
|
|
|
private $filetype_checkers = [];
|
2016-06-20 06:38:13 +02:00
|
|
|
|
2018-02-12 02:56:34 +01:00
|
|
|
/**
|
|
|
|
* @var array<string, string>
|
|
|
|
*/
|
|
|
|
private $filetype_scanner_paths = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array<string, string>
|
|
|
|
*/
|
|
|
|
private $filetype_checker_paths = [];
|
|
|
|
|
2016-09-09 22:21:49 +02:00
|
|
|
/**
|
2016-12-30 02:07:42 +01:00
|
|
|
* @var array<string, IssueHandler>
|
2016-09-09 22:21:49 +02:00
|
|
|
*/
|
2017-02-12 00:56:38 +01:00
|
|
|
private $issue_handlers = [];
|
2016-06-10 20:47:44 +02:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @var array<int, string>
|
|
|
|
*/
|
2017-02-12 00:56:38 +01:00
|
|
|
private $mock_classes = [];
|
2016-06-06 07:07:50 +02:00
|
|
|
|
2017-02-01 01:21:33 +01:00
|
|
|
/**
|
|
|
|
* @var array<int, string>
|
|
|
|
*/
|
2017-02-12 00:56:38 +01:00
|
|
|
private $stub_files = [];
|
2017-02-01 01:21:33 +01:00
|
|
|
|
2017-02-02 18:39:39 +01:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $cache_file_hashes_during_run = true;
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
2017-05-27 02:16:18 +02:00
|
|
|
* @var bool
|
2016-11-02 07:29:00 +01:00
|
|
|
*/
|
2016-09-01 05:12:35 +02:00
|
|
|
public $hide_external_errors = true;
|
|
|
|
|
2016-12-07 00:27:22 +01:00
|
|
|
/** @var bool */
|
2016-12-06 22:41:42 +01:00
|
|
|
public $allow_includes = true;
|
|
|
|
|
2016-12-19 01:17:39 +01:00
|
|
|
/** @var bool */
|
|
|
|
public $totally_typed = false;
|
|
|
|
|
2016-12-24 03:30:32 +01:00
|
|
|
/** @var bool */
|
|
|
|
public $strict_binary_operands = false;
|
|
|
|
|
2017-01-24 08:28:54 +01:00
|
|
|
/** @var bool */
|
|
|
|
public $add_void_docblocks = true;
|
|
|
|
|
2017-01-28 21:17:14 +01:00
|
|
|
/**
|
|
|
|
* If true, assert() calls can be used to check types of variables
|
|
|
|
*
|
2017-05-27 02:16:18 +02:00
|
|
|
* @var bool
|
2017-01-28 21:17:14 +01:00
|
|
|
*/
|
|
|
|
public $use_assert_for_type = false;
|
|
|
|
|
2017-04-15 03:32:14 +02:00
|
|
|
/**
|
2017-05-27 02:16:18 +02:00
|
|
|
* @var bool
|
2017-04-15 03:32:14 +02:00
|
|
|
*/
|
|
|
|
public $remember_property_assignments_after_call = true;
|
|
|
|
|
2017-10-15 18:38:47 +02:00
|
|
|
/** @var bool */
|
|
|
|
public $use_igbinary = false;
|
|
|
|
|
2018-02-01 07:10:27 +01:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $allow_phpstorm_generics = false;
|
|
|
|
|
2018-03-06 17:20:54 +01:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $allow_coercion_from_string_to_class_const = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $allow_string_standin_for_class = true;
|
|
|
|
|
2016-06-18 20:45:55 +02:00
|
|
|
/**
|
2018-02-12 02:56:34 +01:00
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
private $plugin_paths = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Static methods to be called after method checks have completed
|
|
|
|
*
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
public $after_method_checks = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Static methods to be called after expression checks have completed
|
2016-11-02 07:29:00 +01:00
|
|
|
*
|
2018-02-12 02:56:34 +01:00
|
|
|
* @var string[]
|
2016-06-18 20:45:55 +02:00
|
|
|
*/
|
2018-02-12 02:56:34 +01:00
|
|
|
public $after_expression_checks = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Static methods to be called after statement checks have completed
|
|
|
|
*
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
public $after_statement_checks = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Static methods to be called after classlike exists checks have completed
|
|
|
|
*
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
public $after_classlike_exists_checks = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Static methods to be called after classlikes have been scanned
|
|
|
|
*
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
public $after_visit_classlikes = [];
|
2016-06-18 20:45:55 +02:00
|
|
|
|
2016-11-21 03:49:06 +01:00
|
|
|
/** @var array<string, mixed> */
|
2017-02-12 00:56:38 +01:00
|
|
|
private $predefined_constants;
|
2016-11-21 03:49:06 +01:00
|
|
|
|
2017-01-16 02:11:02 +01:00
|
|
|
/** @var array<string, bool> */
|
2017-02-12 00:56:38 +01:00
|
|
|
private $predefined_functions = [];
|
2017-01-16 02:11:02 +01:00
|
|
|
|
2018-03-03 21:19:05 +01:00
|
|
|
/** @var ClassLoader|null */
|
|
|
|
private $composer_class_loader;
|
|
|
|
|
2016-12-14 18:28:38 +01:00
|
|
|
protected function __construct()
|
2016-06-06 07:07:50 +02:00
|
|
|
{
|
2018-01-21 19:38:51 +01:00
|
|
|
self::$instance = $this;
|
2016-06-06 07:07:50 +02:00
|
|
|
}
|
|
|
|
|
2018-01-21 16:22:04 +01:00
|
|
|
/**
|
|
|
|
* Gets a Config object from an XML file.
|
|
|
|
*
|
|
|
|
* Searches up a folder hierarchy for the most immediate config.
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param string $base_dir
|
|
|
|
* @param string $output_format
|
|
|
|
*
|
|
|
|
* @throws ConfigException if a config path is not found
|
|
|
|
*
|
|
|
|
* @return Config
|
|
|
|
*/
|
|
|
|
public static function getConfigForPath($path, $base_dir, $output_format)
|
|
|
|
{
|
|
|
|
$dir_path = realpath($path);
|
|
|
|
|
|
|
|
if ($dir_path === false) {
|
|
|
|
throw new ConfigException('Config not found for path ' . $path);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_dir($dir_path)) {
|
|
|
|
$dir_path = dirname($dir_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
$config = null;
|
|
|
|
|
|
|
|
do {
|
|
|
|
$maybe_path = $dir_path . DIRECTORY_SEPARATOR . Config::DEFAULT_FILE_NAME;
|
|
|
|
|
|
|
|
if (file_exists($maybe_path)) {
|
|
|
|
$config = self::loadFromXMLFile($maybe_path, $base_dir);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$dir_path = dirname($dir_path);
|
|
|
|
} while (dirname($dir_path) !== $dir_path);
|
|
|
|
|
|
|
|
if (!$config) {
|
|
|
|
if ($output_format === ProjectChecker::TYPE_CONSOLE) {
|
|
|
|
exit(
|
|
|
|
'Could not locate a config XML file in path ' . $path . '. Have you run \'psalm --init\' ?' .
|
|
|
|
PHP_EOL
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new ConfigException('Config not found for path ' . $path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $config;
|
|
|
|
}
|
|
|
|
|
2016-06-26 19:45:20 +02:00
|
|
|
/**
|
|
|
|
* Creates a new config object from the file
|
2016-11-02 07:29:00 +01:00
|
|
|
*
|
2017-02-01 01:21:33 +01:00
|
|
|
* @param string $file_path
|
2017-05-04 20:25:58 +02:00
|
|
|
* @param string $base_dir
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-12-29 16:24:10 +01:00
|
|
|
* @return self
|
|
|
|
*/
|
2017-05-04 20:25:58 +02:00
|
|
|
public static function loadFromXMLFile($file_path, $base_dir)
|
2016-12-29 16:24:10 +01:00
|
|
|
{
|
|
|
|
$file_contents = file_get_contents($file_path);
|
|
|
|
|
|
|
|
if ($file_contents === false) {
|
|
|
|
throw new \InvalidArgumentException('Cannot open ' . $file_path);
|
|
|
|
}
|
|
|
|
|
2018-01-28 18:43:19 +01:00
|
|
|
return self::loadFromXML($base_dir, $file_contents);
|
2016-12-29 16:24:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new config object from an XML string
|
|
|
|
*
|
2017-05-04 20:25:58 +02:00
|
|
|
* @param string $base_dir
|
2017-02-01 01:21:33 +01:00
|
|
|
* @param string $file_contents
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-01 05:39:41 +01:00
|
|
|
* @return self
|
2016-11-05 02:14:04 +01:00
|
|
|
* @psalm-suppress MixedArgument
|
|
|
|
* @psalm-suppress MixedPropertyFetch
|
|
|
|
* @psalm-suppress MixedMethodCall
|
2016-12-17 06:48:31 +01:00
|
|
|
* @psalm-suppress MixedAssignment
|
2016-12-24 12:03:55 +01:00
|
|
|
* @psalm-suppress MixedOperand
|
2017-02-24 01:36:51 +01:00
|
|
|
* @psalm-suppress MixedPropertyAssignment
|
2016-06-26 19:45:20 +02:00
|
|
|
*/
|
2018-01-28 18:43:19 +01:00
|
|
|
public static function loadFromXML($base_dir, $file_contents)
|
2016-06-10 00:08:25 +02:00
|
|
|
{
|
2017-02-01 01:21:33 +01:00
|
|
|
$config = new static();
|
2016-06-21 01:30:38 +02:00
|
|
|
|
2017-05-04 20:25:58 +02:00
|
|
|
$config->base_dir = $base_dir;
|
2016-06-10 20:47:44 +02:00
|
|
|
|
2016-12-30 04:11:10 +01:00
|
|
|
$schema_path = dirname(dirname(__DIR__)) . '/config.xsd';
|
|
|
|
|
|
|
|
if (!file_exists($schema_path)) {
|
|
|
|
throw new ConfigException('Cannot locate config schema');
|
|
|
|
}
|
|
|
|
|
|
|
|
$dom_document = new \DOMDocument();
|
|
|
|
$dom_document->loadXML($file_contents);
|
|
|
|
|
|
|
|
// Enable user error handling
|
|
|
|
libxml_use_internal_errors(true);
|
|
|
|
|
|
|
|
if (!$dom_document->schemaValidate($schema_path)) {
|
|
|
|
$errors = libxml_get_errors();
|
|
|
|
foreach ($errors as $error) {
|
|
|
|
if ($error->level === LIBXML_ERR_FATAL || $error->level === LIBXML_ERR_ERROR) {
|
2017-04-28 06:31:55 +02:00
|
|
|
throw new ConfigException(
|
|
|
|
'Error parsing file ' . $error->file . ' on line ' . $error->line . ': ' . $error->message
|
|
|
|
);
|
2016-12-30 04:11:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
libxml_clear_errors();
|
|
|
|
}
|
|
|
|
|
2016-06-10 00:08:25 +02:00
|
|
|
$config_xml = new SimpleXMLElement($file_contents);
|
|
|
|
|
2016-06-25 00:18:11 +02:00
|
|
|
if (isset($config_xml['useDocblockTypes'])) {
|
|
|
|
$attribute_text = (string) $config_xml['useDocblockTypes'];
|
|
|
|
$config->use_docblock_types = $attribute_text === 'true' || $attribute_text === '1';
|
2016-06-10 00:08:25 +02:00
|
|
|
}
|
|
|
|
|
2018-01-13 06:32:20 +01:00
|
|
|
if (isset($config_xml['useDocblockPropertyTypes'])) {
|
|
|
|
$attribute_text = (string) $config_xml['useDocblockPropertyTypes'];
|
|
|
|
$config->use_docblock_property_types = $attribute_text === 'true' || $attribute_text === '1';
|
|
|
|
}
|
|
|
|
|
2016-06-27 04:40:57 +02:00
|
|
|
if (isset($config_xml['throwExceptionOnError'])) {
|
|
|
|
$attribute_text = (string) $config_xml['throwExceptionOnError'];
|
|
|
|
$config->throw_exception = $attribute_text === 'true' || $attribute_text === '1';
|
|
|
|
}
|
|
|
|
|
2016-09-01 05:12:35 +02:00
|
|
|
if (isset($config_xml['hideExternalErrors'])) {
|
|
|
|
$attribute_text = (string) $config_xml['hideExternalErrors'];
|
|
|
|
$config->hide_external_errors = $attribute_text === 'true' || $attribute_text === '1';
|
|
|
|
}
|
|
|
|
|
2016-07-26 21:00:40 +02:00
|
|
|
if (isset($config_xml['autoloader'])) {
|
|
|
|
$config->autoloader = (string) $config_xml['autoloader'];
|
|
|
|
}
|
|
|
|
|
2016-11-04 22:45:12 +01:00
|
|
|
if (isset($config_xml['cacheDirectory'])) {
|
2017-01-16 21:58:35 +01:00
|
|
|
$config->cache_directory = (string)$config_xml['cacheDirectory'];
|
2017-01-15 23:20:54 +01:00
|
|
|
} else {
|
|
|
|
$config->cache_directory = sys_get_temp_dir() . '/psalm';
|
2016-11-04 22:45:12 +01:00
|
|
|
}
|
|
|
|
|
2017-01-16 21:58:35 +01:00
|
|
|
if (@mkdir($config->cache_directory, 0777, true) === false && is_dir($config->cache_directory) === false) {
|
|
|
|
trigger_error('Could not create cache directory: ' . $config->cache_directory, E_USER_ERROR);
|
|
|
|
}
|
|
|
|
|
2016-12-06 22:41:42 +01:00
|
|
|
if (isset($config_xml['allowFileIncludes'])) {
|
|
|
|
$attribute_text = (string) $config_xml['allowFileIncludes'];
|
|
|
|
$config->allow_includes = $attribute_text === 'true' || $attribute_text === '1';
|
|
|
|
}
|
|
|
|
|
2016-12-19 01:17:39 +01:00
|
|
|
if (isset($config_xml['totallyTyped'])) {
|
|
|
|
$attribute_text = (string) $config_xml['totallyTyped'];
|
|
|
|
$config->totally_typed = $attribute_text === 'true' || $attribute_text === '1';
|
|
|
|
}
|
|
|
|
|
2016-12-24 03:30:32 +01:00
|
|
|
if (isset($config_xml['strictBinaryOperands'])) {
|
|
|
|
$attribute_text = (string) $config_xml['strictBinaryOperands'];
|
|
|
|
$config->strict_binary_operands = $attribute_text === 'true' || $attribute_text === '1';
|
|
|
|
}
|
|
|
|
|
2017-02-01 16:13:37 +01:00
|
|
|
if (isset($config_xml['requireVoidReturnType'])) {
|
|
|
|
$attribute_text = (string) $config_xml['requireVoidReturnType'];
|
2017-01-24 08:28:54 +01:00
|
|
|
$config->add_void_docblocks = $attribute_text === 'true' || $attribute_text === '1';
|
|
|
|
}
|
|
|
|
|
2017-01-28 21:17:14 +01:00
|
|
|
if (isset($config_xml['useAssertForType'])) {
|
|
|
|
$attribute_text = (string) $config_xml['useAssertForType'];
|
|
|
|
$config->use_assert_for_type = $attribute_text === 'true' || $attribute_text === '1';
|
|
|
|
}
|
|
|
|
|
2017-02-02 18:39:39 +01:00
|
|
|
if (isset($config_xml['cacheFileContentHashes'])) {
|
|
|
|
$attribute_text = (string) $config_xml['cacheFileContentHashes'];
|
|
|
|
$config->cache_file_hashes_during_run = $attribute_text === 'true' || $attribute_text === '1';
|
|
|
|
}
|
|
|
|
|
2017-04-15 03:32:14 +02:00
|
|
|
if (isset($config_xml['rememberPropertyAssignmentsAfterCall'])) {
|
|
|
|
$attribute_text = (string) $config_xml['rememberPropertyAssignmentsAfterCall'];
|
|
|
|
$config->remember_property_assignments_after_call = $attribute_text === 'true' || $attribute_text === '1';
|
|
|
|
}
|
|
|
|
|
2017-10-15 18:38:47 +02:00
|
|
|
if (isset($config_xml['serializer'])) {
|
|
|
|
$attribute_text = (string) $config_xml['serializer'];
|
|
|
|
$config->use_igbinary = $attribute_text === 'igbinary';
|
2018-02-19 17:14:07 +01:00
|
|
|
} elseif ($igbinary_version = phpversion('igbinary')) {
|
|
|
|
$config->use_igbinary = version_compare($igbinary_version, '2.0.5') >= 0;
|
2017-10-15 18:38:47 +02:00
|
|
|
}
|
|
|
|
|
2018-02-01 07:10:27 +01:00
|
|
|
if (isset($config_xml['allowPhpStormGenerics'])) {
|
|
|
|
$attribute_text = (string) $config_xml['allowPhpStormGenerics'];
|
|
|
|
$config->allow_phpstorm_generics = $attribute_text === 'true' || $attribute_text === '1';
|
|
|
|
}
|
|
|
|
|
2018-03-06 17:20:54 +01:00
|
|
|
if (isset($config_xml['allowCoercionFromStringToClassConst'])) {
|
|
|
|
$attribute_text = (string) $config_xml['allowCoercionFromStringToClassConst'];
|
|
|
|
$config->allow_coercion_from_string_to_class_const = $attribute_text === 'true' || $attribute_text === '1';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($config_xml['allowStringToStandInForClass'])) {
|
|
|
|
$attribute_text = (string) $config_xml['allowCoercionFromStringToClassConst'];
|
|
|
|
$config->allow_string_standin_for_class = $attribute_text === 'true' || $attribute_text === '1';
|
|
|
|
}
|
|
|
|
|
2016-12-29 14:42:39 +01:00
|
|
|
if (isset($config_xml->projectFiles)) {
|
2017-05-04 20:25:58 +02:00
|
|
|
$config->project_files = ProjectFileFilter::loadFromXMLElement($config_xml->projectFiles, $base_dir, true);
|
2016-06-10 00:08:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($config_xml->fileExtensions)) {
|
2016-06-21 01:30:38 +02:00
|
|
|
$config->file_extensions = [];
|
2016-06-10 20:47:44 +02:00
|
|
|
|
2016-06-21 01:30:38 +02:00
|
|
|
$config->loadFileExtensions($config_xml->fileExtensions->extension);
|
2016-06-10 20:47:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($config_xml->mockClasses) && isset($config_xml->mockClasses->class)) {
|
2016-11-22 01:07:56 +01:00
|
|
|
/** @var \SimpleXMLElement $mock_class */
|
2016-06-10 20:47:44 +02:00
|
|
|
foreach ($config_xml->mockClasses->class as $mock_class) {
|
2017-06-13 06:51:16 +02:00
|
|
|
$config->mock_classes[] = (string)$mock_class['name'];
|
2016-06-10 00:08:25 +02:00
|
|
|
}
|
2016-06-10 20:47:44 +02:00
|
|
|
}
|
|
|
|
|
2017-02-01 01:21:33 +01:00
|
|
|
if (isset($config_xml->stubs) && isset($config_xml->stubs->file)) {
|
|
|
|
/** @var \SimpleXMLElement $stub_file */
|
|
|
|
foreach ($config_xml->stubs->file as $stub_file) {
|
2018-03-04 01:05:15 +01:00
|
|
|
$file_path = realpath($config->base_dir . DIRECTORY_SEPARATOR . $stub_file['name']);
|
2017-02-01 01:21:33 +01:00
|
|
|
|
|
|
|
if (!$file_path) {
|
2017-04-28 06:31:55 +02:00
|
|
|
throw new Exception\ConfigException(
|
2018-03-04 01:05:15 +01:00
|
|
|
'Cannot resolve stubfile path ' . $config->base_dir . DIRECTORY_SEPARATOR . $stub_file['name']
|
2017-04-28 06:31:55 +02:00
|
|
|
);
|
2017-02-01 01:21:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$config->stub_files[] = $file_path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-18 20:45:55 +02:00
|
|
|
// this plugin loading system borrows heavily from etsy/phan
|
|
|
|
if (isset($config_xml->plugins) && isset($config_xml->plugins->plugin)) {
|
2016-11-22 01:07:56 +01:00
|
|
|
/** @var \SimpleXMLElement $plugin */
|
2016-06-18 20:45:55 +02:00
|
|
|
foreach ($config_xml->plugins->plugin as $plugin) {
|
|
|
|
$plugin_file_name = $plugin['filename'];
|
2016-06-20 06:38:13 +02:00
|
|
|
|
2016-06-21 01:30:38 +02:00
|
|
|
$path = $config->base_dir . $plugin_file_name;
|
2016-06-20 06:38:13 +02:00
|
|
|
|
2018-01-02 02:05:54 +01:00
|
|
|
$config->addPluginPath($path);
|
2016-06-18 20:45:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-30 02:07:42 +01:00
|
|
|
if (isset($config_xml->issueHandlers)) {
|
2016-11-22 01:07:56 +01:00
|
|
|
/** @var \SimpleXMLElement $issue_handler */
|
2016-12-30 02:07:42 +01:00
|
|
|
foreach ($config_xml->issueHandlers->children() as $key => $issue_handler) {
|
2017-02-24 01:36:51 +01:00
|
|
|
/** @var string $key */
|
2017-05-04 20:25:58 +02:00
|
|
|
$config->issue_handlers[$key] = IssueHandler::loadFromXMLElement($issue_handler, $base_dir);
|
2016-06-10 00:08:25 +02:00
|
|
|
}
|
|
|
|
}
|
2016-06-26 19:45:20 +02:00
|
|
|
|
2017-02-01 01:21:33 +01:00
|
|
|
if ($config->autoloader) {
|
2017-10-29 19:39:23 +01:00
|
|
|
/** @psalm-suppress UnresolvableInclude */
|
2017-05-04 20:25:58 +02:00
|
|
|
require_once($base_dir . DIRECTORY_SEPARATOR . $config->autoloader);
|
2017-02-01 01:21:33 +01:00
|
|
|
}
|
|
|
|
|
2017-02-01 20:56:10 +01:00
|
|
|
$config->collectPredefinedConstants();
|
|
|
|
$config->collectPredefinedFunctions();
|
|
|
|
|
2016-06-26 19:45:20 +02:00
|
|
|
return $config;
|
2016-06-10 00:08:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return $this
|
|
|
|
*/
|
2016-06-06 07:07:50 +02:00
|
|
|
public static function getInstance()
|
|
|
|
{
|
2018-01-21 19:38:51 +01:00
|
|
|
if (self::$instance) {
|
|
|
|
return self::$instance;
|
2016-06-06 07:07:50 +02:00
|
|
|
}
|
|
|
|
|
2018-01-21 19:38:51 +01:00
|
|
|
throw new \UnexpectedValueException('No config initialized');
|
2016-06-06 07:07:50 +02:00
|
|
|
}
|
|
|
|
|
2018-03-03 21:25:35 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-03-03 21:19:05 +01:00
|
|
|
public function setComposerClassLoader(ClassLoader $loader)
|
|
|
|
{
|
|
|
|
$this->composer_class_loader = $loader;
|
|
|
|
}
|
|
|
|
|
2016-12-09 04:37:59 +01:00
|
|
|
/**
|
|
|
|
* @param string $issue_key
|
|
|
|
* @param string $error_level
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-12-09 04:37:59 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setCustomErrorLevel($issue_key, $error_level)
|
|
|
|
{
|
2016-12-30 02:07:42 +01:00
|
|
|
$this->issue_handlers[$issue_key] = new IssueHandler();
|
|
|
|
$this->issue_handlers[$issue_key]->setErrorLevel($error_level);
|
2016-12-09 04:37:59 +01:00
|
|
|
}
|
|
|
|
|
2016-10-30 17:46:18 +01:00
|
|
|
/**
|
2018-01-02 03:17:23 +01:00
|
|
|
* @param array<SimpleXMLElement> $extensions
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
|
|
|
* @throws ConfigException if a Config file could not be found
|
|
|
|
*
|
2016-10-30 17:46:18 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2017-02-12 00:56:38 +01:00
|
|
|
private function loadFileExtensions($extensions)
|
2016-06-20 06:38:13 +02:00
|
|
|
{
|
|
|
|
foreach ($extensions as $extension) {
|
2016-11-05 02:14:04 +01:00
|
|
|
$extension_name = preg_replace('/^\.?/', '', (string)$extension['name']);
|
2016-06-20 06:38:13 +02:00
|
|
|
$this->file_extensions[] = $extension_name;
|
|
|
|
|
2018-01-21 19:38:51 +01:00
|
|
|
if (isset($extension['scanner'])) {
|
|
|
|
$path = $this->base_dir . (string)$extension['scanner'];
|
|
|
|
|
|
|
|
if (!file_exists($path)) {
|
|
|
|
throw new Exception\ConfigException('Error parsing config: cannot find file ' . $path);
|
|
|
|
}
|
|
|
|
|
2018-02-12 02:56:34 +01:00
|
|
|
$this->filetype_scanner_paths[$extension_name] = $path;
|
2018-01-21 19:38:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($extension['checker'])) {
|
|
|
|
$path = $this->base_dir . (string)$extension['checker'];
|
2016-06-20 06:38:13 +02:00
|
|
|
|
|
|
|
if (!file_exists($path)) {
|
2016-06-20 07:05:44 +02:00
|
|
|
throw new Exception\ConfigException('Error parsing config: cannot find file ' . $path);
|
2016-06-20 06:38:13 +02:00
|
|
|
}
|
|
|
|
|
2018-02-12 02:56:34 +01:00
|
|
|
$this->filetype_checker_paths[$extension_name] = $path;
|
2017-01-02 21:31:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-20 06:38:13 +02:00
|
|
|
|
2018-02-12 02:56:34 +01:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function addPluginPath($path)
|
|
|
|
{
|
|
|
|
if (!file_exists($path)) {
|
|
|
|
throw new \InvalidArgumentException('Cannot find plugin file ' . $path);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->plugin_paths[] = $path;
|
|
|
|
}
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
/**
|
|
|
|
* Initialises all the plugins (done once the config is fully loaded)
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-01-02 21:31:18 +01:00
|
|
|
* @return void
|
|
|
|
* @psalm-suppress MixedArrayAccess
|
|
|
|
* @psalm-suppress MixedAssignment
|
|
|
|
* @psalm-suppress MixedOperand
|
|
|
|
*/
|
|
|
|
public function initializePlugins(ProjectChecker $project_checker)
|
|
|
|
{
|
2018-01-21 19:38:51 +01:00
|
|
|
$codebase = $project_checker->codebase;
|
|
|
|
|
2018-02-12 02:56:34 +01:00
|
|
|
foreach ($this->filetype_scanner_paths as $extension => $path) {
|
|
|
|
$fq_class_name = $this->getPluginClassForPath($project_checker, $path, 'Psalm\\Scanner\\FileScanner');
|
2018-01-21 19:38:51 +01:00
|
|
|
|
2018-02-12 02:56:34 +01:00
|
|
|
$this->filetype_scanners[$extension] = $fq_class_name;
|
2018-01-21 19:38:51 +01:00
|
|
|
|
|
|
|
/** @psalm-suppress UnresolvableInclude */
|
|
|
|
require_once($path);
|
2018-02-12 02:56:34 +01:00
|
|
|
}
|
2018-01-21 19:38:51 +01:00
|
|
|
|
2018-02-12 02:56:34 +01:00
|
|
|
foreach ($this->filetype_checker_paths as $extension => $path) {
|
|
|
|
$fq_class_name = $this->getPluginClassForPath($project_checker, $path, 'Psalm\\Checker\\FileChecker');
|
|
|
|
|
|
|
|
$this->filetype_checkers[$extension] = $fq_class_name;
|
2018-01-21 19:38:51 +01:00
|
|
|
|
2018-02-12 02:56:34 +01:00
|
|
|
/** @psalm-suppress UnresolvableInclude */
|
|
|
|
require_once($path);
|
2018-01-21 19:38:51 +01:00
|
|
|
}
|
|
|
|
|
2018-02-12 02:56:34 +01:00
|
|
|
foreach ($this->plugin_paths as $path) {
|
|
|
|
$fq_class_name = $this->getPluginClassForPath($project_checker, $path, 'Psalm\\Plugin');
|
|
|
|
|
|
|
|
/** @psalm-suppress UnresolvableInclude */
|
|
|
|
require_once($path);
|
2016-06-20 06:38:13 +02:00
|
|
|
|
2018-02-12 02:56:34 +01:00
|
|
|
if ($codebase->methods->methodExists($fq_class_name . '::afterMethodCallCheck')) {
|
2018-02-12 04:49:19 +01:00
|
|
|
$this->after_method_checks[$fq_class_name] = $fq_class_name;
|
2018-02-12 02:56:34 +01:00
|
|
|
}
|
2016-06-20 06:38:13 +02:00
|
|
|
|
2018-02-12 02:56:34 +01:00
|
|
|
if ($codebase->methods->methodExists($fq_class_name . '::afterExpressionCheck')) {
|
2018-02-12 04:49:19 +01:00
|
|
|
$this->after_expression_checks[$fq_class_name] = $fq_class_name;
|
2017-01-02 21:31:18 +01:00
|
|
|
}
|
|
|
|
|
2018-02-12 02:56:34 +01:00
|
|
|
if ($codebase->methods->methodExists($fq_class_name . '::afterStatementCheck')) {
|
2018-02-12 04:49:19 +01:00
|
|
|
$this->after_statement_checks[$fq_class_name] = $fq_class_name;
|
2018-02-12 02:56:34 +01:00
|
|
|
}
|
2016-06-20 06:38:13 +02:00
|
|
|
|
2018-02-12 02:56:34 +01:00
|
|
|
if ($codebase->methods->methodExists($fq_class_name . '::afterClassLikeExistsCheck')) {
|
2018-02-12 04:49:19 +01:00
|
|
|
$this->after_classlike_exists_checks[$fq_class_name] = $fq_class_name;
|
2016-06-20 06:38:13 +02:00
|
|
|
}
|
2017-01-02 21:31:18 +01:00
|
|
|
|
2018-02-12 02:56:34 +01:00
|
|
|
if ($codebase->methods->methodExists($fq_class_name . '::afterVisitClassLike')) {
|
2018-02-12 04:49:19 +01:00
|
|
|
$this->after_visit_classlikes[$fq_class_name] = $fq_class_name;
|
2018-02-12 02:56:34 +01:00
|
|
|
}
|
2016-06-20 06:38:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-12 02:56:34 +01:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @param string $must_extend
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function getPluginClassForPath(ProjectChecker $project_checker, $path, $must_extend)
|
|
|
|
{
|
|
|
|
$codebase = $project_checker->codebase;
|
|
|
|
|
|
|
|
$file_storage = $codebase->createFileStorageForPath($path);
|
2018-02-21 19:54:11 +01:00
|
|
|
$file_to_scan = new FileScanner($path, $this->shortenFileName($path), true);
|
2018-02-12 02:56:34 +01:00
|
|
|
$file_to_scan->scan(
|
|
|
|
$codebase,
|
|
|
|
$file_storage
|
|
|
|
);
|
|
|
|
|
|
|
|
$declared_classes = ClassLikeChecker::getClassesForFile($project_checker, $path);
|
|
|
|
|
|
|
|
if (count($declared_classes) !== 1) {
|
|
|
|
throw new \InvalidArgumentException(
|
|
|
|
'Plugins must have exactly one class in the file - ' . $path . ' has ' .
|
|
|
|
count($declared_classes)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-02-12 04:49:19 +01:00
|
|
|
$fq_class_name = reset($declared_classes);
|
|
|
|
|
2018-02-12 02:56:34 +01:00
|
|
|
if (!$codebase->classExtends(
|
2018-02-12 04:49:19 +01:00
|
|
|
$fq_class_name,
|
2018-02-12 02:56:34 +01:00
|
|
|
$must_extend
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
throw new \InvalidArgumentException(
|
|
|
|
'This plugin must extend ' . $must_extend . ' - ' . $path . ' does not'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-02-12 04:49:19 +01:00
|
|
|
return $fq_class_name;
|
2018-02-12 02:56:34 +01:00
|
|
|
}
|
|
|
|
|
2016-10-09 23:55:21 +02:00
|
|
|
/**
|
|
|
|
* @param string $file_name
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-10-09 23:55:21 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-06-10 23:20:04 +02:00
|
|
|
public function shortenFileName($file_name)
|
|
|
|
{
|
2017-01-18 04:10:21 +01:00
|
|
|
return preg_replace('/^' . preg_quote($this->base_dir, DIRECTORY_SEPARATOR) . '/', '', $file_name);
|
2016-06-10 23:20:04 +02:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @param string $issue_type
|
2017-01-16 18:59:09 +01:00
|
|
|
* @param string $file_path
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
2017-03-24 23:34:46 +01:00
|
|
|
public function reportIssueInFile($issue_type, $file_path)
|
2016-06-10 00:08:25 +02:00
|
|
|
{
|
2017-05-27 02:05:57 +02:00
|
|
|
if (!$this->totally_typed && in_array($issue_type, self::$MIXED_ISSUES, true)) {
|
2017-03-24 23:34:46 +01:00
|
|
|
return false;
|
2016-12-19 01:17:39 +01:00
|
|
|
}
|
|
|
|
|
2017-03-24 23:34:46 +01:00
|
|
|
if ($this->hide_external_errors) {
|
2018-01-21 19:38:51 +01:00
|
|
|
$codebase = ProjectChecker::getInstance()->codebase;
|
2017-03-24 23:34:46 +01:00
|
|
|
|
2018-02-04 00:52:35 +01:00
|
|
|
if (!$codebase->analyzer->canReportIssues($file_path)) {
|
2017-03-24 23:34:46 +01:00
|
|
|
return false;
|
2016-08-05 21:11:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-16 18:59:09 +01:00
|
|
|
if ($this->getReportingLevelForFile($issue_type, $file_path) === self::REPORT_SUPPRESS) {
|
2017-03-24 23:34:46 +01:00
|
|
|
return false;
|
2016-06-10 20:47:44 +02:00
|
|
|
}
|
2016-06-10 00:08:25 +02:00
|
|
|
|
2017-03-24 23:34:46 +01:00
|
|
|
return true;
|
2016-06-10 00:08:25 +02:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
2017-01-02 21:31:18 +01:00
|
|
|
* @param string $file_path
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
2017-01-02 21:31:18 +01:00
|
|
|
public function isInProjectDirs($file_path)
|
2016-09-01 05:12:35 +02:00
|
|
|
{
|
2017-01-02 21:31:18 +01:00
|
|
|
return $this->project_files && $this->project_files->allows($file_path);
|
2016-09-01 05:12:35 +02:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @param string $issue_type
|
2017-01-16 18:59:09 +01:00
|
|
|
* @param string $file_path
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2017-01-16 18:59:09 +01:00
|
|
|
public function getReportingLevelForFile($issue_type, $file_path)
|
2016-06-21 01:30:38 +02:00
|
|
|
{
|
2016-12-30 02:07:42 +01:00
|
|
|
if (isset($this->issue_handlers[$issue_type])) {
|
2017-01-16 18:59:09 +01:00
|
|
|
return $this->issue_handlers[$issue_type]->getReportingLevelForFile($file_path);
|
2016-06-21 01:30:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return self::REPORT_ERROR;
|
|
|
|
}
|
|
|
|
|
2018-03-21 03:36:03 +01:00
|
|
|
/**
|
|
|
|
* @param string $issue_type
|
|
|
|
* @param string $fq_classlike_name
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getReportingLevelForClass($issue_type, $fq_classlike_name)
|
|
|
|
{
|
|
|
|
if (isset($this->issue_handlers[$issue_type])) {
|
|
|
|
return $this->issue_handlers[$issue_type]->getReportingLevelForClass($fq_classlike_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::REPORT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $issue_type
|
|
|
|
* @param string $method_id
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getReportingLevelForMethod($issue_type, $method_id)
|
|
|
|
{
|
|
|
|
if (isset($this->issue_handlers[$issue_type])) {
|
|
|
|
return $this->issue_handlers[$issue_type]->getReportingLevelForMethod($method_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::REPORT_ERROR;
|
|
|
|
}
|
|
|
|
|
2016-10-15 06:12:57 +02:00
|
|
|
/**
|
|
|
|
* @return array<string>
|
|
|
|
*/
|
2016-12-29 16:24:10 +01:00
|
|
|
public function getProjectDirectories()
|
2016-06-10 00:08:25 +02:00
|
|
|
{
|
2016-12-29 14:42:39 +01:00
|
|
|
if (!$this->project_files) {
|
2016-08-07 02:27:13 +02:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2016-12-29 16:24:10 +01:00
|
|
|
return $this->project_files->getDirectories();
|
2016-06-13 21:33:18 +02:00
|
|
|
}
|
2016-06-10 00:08:25 +02:00
|
|
|
|
2016-10-15 06:12:57 +02:00
|
|
|
/**
|
|
|
|
* @return array<string>
|
|
|
|
*/
|
2016-06-13 21:33:18 +02:00
|
|
|
public function getFileExtensions()
|
|
|
|
{
|
|
|
|
return $this->file_extensions;
|
2016-06-10 00:08:25 +02:00
|
|
|
}
|
2016-06-10 20:47:44 +02:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
2017-02-24 01:36:51 +01:00
|
|
|
* @return array<string, string>
|
2016-11-02 07:29:00 +01:00
|
|
|
*/
|
2018-01-21 19:38:51 +01:00
|
|
|
public function getFiletypeScanners()
|
|
|
|
{
|
|
|
|
return $this->filetype_scanners;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array<string, string>
|
|
|
|
*/
|
|
|
|
public function getFiletypeCheckers()
|
2016-06-20 06:38:13 +02:00
|
|
|
{
|
2018-01-21 19:38:51 +01:00
|
|
|
return $this->filetype_checkers;
|
2016-06-20 06:38:13 +02:00
|
|
|
}
|
|
|
|
|
2016-10-09 23:55:21 +02:00
|
|
|
/**
|
2016-11-01 05:39:41 +01:00
|
|
|
* @return array<int, string>
|
2016-10-09 23:55:21 +02:00
|
|
|
*/
|
2016-06-10 20:47:44 +02:00
|
|
|
public function getMockClasses()
|
|
|
|
{
|
|
|
|
return $this->mock_classes;
|
|
|
|
}
|
2016-06-18 20:45:55 +02:00
|
|
|
|
2017-02-01 01:21:33 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-01-21 19:38:51 +01:00
|
|
|
public function visitStubFiles(Codebase $codebase)
|
2017-02-01 01:21:33 +01:00
|
|
|
{
|
2018-01-21 19:38:51 +01:00
|
|
|
$codebase->register_global_functions = true;
|
2017-02-11 00:12:59 +01:00
|
|
|
|
2018-03-08 23:56:52 +01:00
|
|
|
// note: don't realpath $generic_stubs_path, or phar version will fail
|
|
|
|
$generic_stubs_path = __DIR__ . '/Stubs/CoreGenericFunctions.php';
|
2017-02-11 00:12:59 +01:00
|
|
|
|
2018-03-08 23:56:52 +01:00
|
|
|
if (!file_exists($generic_stubs_path)) {
|
2017-02-11 00:12:59 +01:00
|
|
|
throw new \UnexpectedValueException('Cannot locate core generic stubs');
|
|
|
|
}
|
|
|
|
|
2018-03-08 23:56:52 +01:00
|
|
|
// note: don't realpath $generic_classes_path, or phar version will fail
|
2018-03-09 00:01:12 +01:00
|
|
|
$generic_classes_path = __DIR__ . '/Stubs/CoreGenericClasses.php';
|
2018-02-01 05:27:25 +01:00
|
|
|
|
2018-03-08 23:56:52 +01:00
|
|
|
if (!file_exists($generic_classes_path)) {
|
2018-02-01 05:27:25 +01:00
|
|
|
throw new \UnexpectedValueException('Cannot locate core generic classes');
|
|
|
|
}
|
|
|
|
|
|
|
|
$stub_files = array_merge([$generic_stubs_path, $generic_classes_path], $this->stub_files);
|
|
|
|
|
|
|
|
foreach ($stub_files as $stub_file_path) {
|
2018-01-21 19:38:51 +01:00
|
|
|
$file_storage = $codebase->createFileStorageForPath($stub_file_path);
|
2018-01-21 18:44:46 +01:00
|
|
|
$file_to_scan = new FileScanner($stub_file_path, $this->shortenFileName($stub_file_path), false);
|
|
|
|
$file_to_scan->scan(
|
2018-01-21 19:38:51 +01:00
|
|
|
$codebase,
|
2018-01-21 18:44:46 +01:00
|
|
|
$file_storage
|
|
|
|
);
|
2017-02-01 01:21:33 +01:00
|
|
|
}
|
2017-02-11 00:12:59 +01:00
|
|
|
|
2018-01-21 19:38:51 +01:00
|
|
|
$codebase->register_global_functions = false;
|
2017-02-01 01:21:33 +01:00
|
|
|
}
|
|
|
|
|
2016-11-04 22:45:12 +01:00
|
|
|
/**
|
2016-11-05 02:14:04 +01:00
|
|
|
* @return string
|
2016-11-04 22:45:12 +01:00
|
|
|
*/
|
|
|
|
public function getCacheDirectory()
|
|
|
|
{
|
|
|
|
return $this->cache_directory;
|
|
|
|
}
|
|
|
|
|
2016-11-21 05:45:10 +01:00
|
|
|
/**
|
|
|
|
* @return array<string, mixed>
|
|
|
|
*/
|
2016-11-21 03:49:06 +01:00
|
|
|
public function getPredefinedConstants()
|
|
|
|
{
|
|
|
|
return $this->predefined_constants;
|
|
|
|
}
|
|
|
|
|
2016-11-21 05:45:10 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
2018-01-10 16:56:43 +01:00
|
|
|
* @psalm-suppress MixedTypeCoercion
|
2016-11-21 05:45:10 +01:00
|
|
|
*/
|
2016-11-21 03:49:06 +01:00
|
|
|
public function collectPredefinedConstants()
|
|
|
|
{
|
|
|
|
$this->predefined_constants = get_defined_constants();
|
|
|
|
}
|
2017-01-16 02:11:02 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array<string, bool>
|
|
|
|
*/
|
|
|
|
public function getPredefinedFunctions()
|
|
|
|
{
|
|
|
|
return $this->predefined_functions;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
* @psalm-suppress InvalidPropertyAssignment
|
|
|
|
* @psalm-suppress MixedAssignment
|
2017-11-19 18:14:02 +01:00
|
|
|
* @psalm-suppress MixedArrayOffset
|
2017-01-16 02:11:02 +01:00
|
|
|
*/
|
|
|
|
public function collectPredefinedFunctions()
|
|
|
|
{
|
|
|
|
$defined_functions = get_defined_functions();
|
|
|
|
|
|
|
|
if (isset($defined_functions['user'])) {
|
|
|
|
foreach ($defined_functions['user'] as $function_name) {
|
|
|
|
$this->predefined_functions[$function_name] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($defined_functions['internal'])) {
|
|
|
|
foreach ($defined_functions['internal'] as $function_name) {
|
|
|
|
$this->predefined_functions[$function_name] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-28 00:56:10 +01:00
|
|
|
|
2018-01-01 17:47:03 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*
|
|
|
|
* @psalm-suppress MixedAssignment
|
|
|
|
* @psalm-suppress MixedArrayAccess
|
|
|
|
*/
|
|
|
|
public function visitComposerAutoloadFiles(ProjectChecker $project_checker)
|
|
|
|
{
|
|
|
|
$composer_json_path = $this->base_dir . 'composer.json'; // this should ideally not be hardcoded
|
|
|
|
|
|
|
|
if (!file_exists($composer_json_path)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @psalm-suppress PossiblyFalseArgument */
|
|
|
|
if (!$composer_json = json_decode(file_get_contents($composer_json_path), true)) {
|
|
|
|
throw new \UnexpectedValueException('Invalid composer.json at ' . $composer_json_path);
|
|
|
|
}
|
|
|
|
|
2018-03-31 02:03:56 +02:00
|
|
|
$autoload_files_files = [];
|
2018-01-21 19:38:51 +01:00
|
|
|
|
2018-03-31 02:03:56 +02:00
|
|
|
if (isset($composer_json['autoload']['files'])) {
|
2018-01-01 17:47:03 +01:00
|
|
|
/** @var string[] */
|
2018-03-31 02:03:56 +02:00
|
|
|
$composer_autoload_files = $composer_json['autoload']['files'];
|
2018-01-01 17:47:03 +01:00
|
|
|
|
2018-03-31 02:03:56 +02:00
|
|
|
foreach ($composer_autoload_files as $file) {
|
2018-01-01 17:47:03 +01:00
|
|
|
$file_path = realpath($this->base_dir . $file);
|
|
|
|
|
2018-03-31 02:03:56 +02:00
|
|
|
if ($file_path && file_exists($file_path)) {
|
|
|
|
$autoload_files_files[] = $file_path;
|
2018-01-01 17:47:03 +01:00
|
|
|
}
|
2018-03-31 02:03:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$vendor_autoload_files_path
|
|
|
|
= $this->base_dir . DIRECTORY_SEPARATOR . 'vendor'
|
|
|
|
. DIRECTORY_SEPARATOR . 'composer' . DIRECTORY_SEPARATOR . 'autoload_files.php';
|
|
|
|
|
|
|
|
if (file_exists($vendor_autoload_files_path)) {
|
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
* @psalm-suppress UnresolvableInclude
|
|
|
|
*/
|
|
|
|
$vendor_autoload_files = require $vendor_autoload_files_path;
|
|
|
|
|
|
|
|
$autoload_files_files = array_merge($autoload_files_files, $vendor_autoload_files);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($autoload_files_files) {
|
|
|
|
$codebase = $project_checker->codebase;
|
|
|
|
$codebase->register_global_functions = true;
|
2018-01-01 17:47:03 +01:00
|
|
|
|
2018-03-31 02:03:56 +02:00
|
|
|
foreach ($autoload_files_files as $file_path) {
|
2018-01-21 19:38:51 +01:00
|
|
|
$file_storage = $codebase->createFileStorageForPath($file_path);
|
|
|
|
$file_to_scan = new \Psalm\Scanner\FileScanner($file_path, $this->shortenFileName($file_path), false);
|
2018-01-21 18:44:46 +01:00
|
|
|
$file_to_scan->scan(
|
2018-01-21 19:38:51 +01:00
|
|
|
$codebase,
|
2018-01-21 18:44:46 +01:00
|
|
|
$file_storage
|
|
|
|
);
|
2018-01-01 17:47:03 +01:00
|
|
|
}
|
|
|
|
|
2018-01-21 19:38:51 +01:00
|
|
|
$project_checker->codebase->register_global_functions = false;
|
|
|
|
}
|
2018-01-01 17:47:03 +01:00
|
|
|
}
|
|
|
|
|
2017-12-28 00:56:10 +01:00
|
|
|
/**
|
2018-03-03 21:19:05 +01:00
|
|
|
* @param string $fq_classlike_name
|
2018-02-22 00:59:31 +01:00
|
|
|
*
|
2018-03-03 21:19:05 +01:00
|
|
|
* @return string|false
|
2017-12-28 00:56:10 +01:00
|
|
|
*/
|
2018-03-03 21:19:05 +01:00
|
|
|
public function getComposerFilePathForClassLike($fq_classlike_name)
|
2017-12-28 00:56:10 +01:00
|
|
|
{
|
2018-03-03 21:19:05 +01:00
|
|
|
if (!$this->composer_class_loader) {
|
2018-03-06 23:30:54 +01:00
|
|
|
return false;
|
2017-12-28 00:56:10 +01:00
|
|
|
}
|
|
|
|
|
2018-03-03 21:19:05 +01:00
|
|
|
return $this->composer_class_loader->findFile($fq_classlike_name);
|
2017-12-28 00:56:10 +01:00
|
|
|
}
|
2017-12-29 18:29:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $dir
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function removeCacheDirectory($dir)
|
|
|
|
{
|
|
|
|
if (is_dir($dir)) {
|
|
|
|
$objects = scandir($dir);
|
|
|
|
|
|
|
|
if ($objects === false) {
|
|
|
|
throw new \UnexpectedValueException('Not expecting false here');
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($objects as $object) {
|
|
|
|
if ($object != '.' && $object != '..') {
|
|
|
|
if (filetype($dir . '/' . $object) == 'dir') {
|
|
|
|
self::removeCacheDirectory($dir . '/' . $object);
|
|
|
|
} else {
|
|
|
|
unlink($dir . '/' . $object);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
reset($objects);
|
|
|
|
rmdir($dir);
|
|
|
|
}
|
|
|
|
}
|
2016-06-06 07:07:50 +02:00
|
|
|
}
|