mirror of
https://github.com/danog/psalm.git
synced 2025-01-21 21:31:13 +01:00
add support for 'COMPOSER' env variable for alternate name of 'composer.json' (#4275)
This commit is contained in:
parent
4eb37fcb0b
commit
0475f379aa
@ -13,6 +13,7 @@ use Psalm\Exception\ConfigException;
|
||||
use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
|
||||
use Psalm\Internal\Analyzer\FileAnalyzer;
|
||||
use Psalm\Internal\Analyzer\ProjectAnalyzer;
|
||||
use Psalm\Internal\Composer;
|
||||
use Psalm\Internal\IncludeCollector;
|
||||
use Psalm\Internal\Scanner\FileScanner;
|
||||
use Psalm\Issue\ArgumentIssue;
|
||||
@ -1993,7 +1994,7 @@ class Config
|
||||
*/
|
||||
private function getPHPVersionFromComposerJson(): ?string
|
||||
{
|
||||
$composer_json_path = $this->base_dir . DIRECTORY_SEPARATOR . 'composer.json';
|
||||
$composer_json_path = Composer::getJsonFilePath($this->base_dir);
|
||||
|
||||
if (file_exists($composer_json_path)) {
|
||||
if (!$composer_json = json_decode(file_get_contents($composer_json_path), true)) {
|
||||
|
@ -1,6 +1,7 @@
|
||||
<?php
|
||||
namespace Psalm\Config;
|
||||
|
||||
use Psalm\Internal\Composer;
|
||||
use function array_merge;
|
||||
use function array_shift;
|
||||
use function array_unique;
|
||||
@ -164,7 +165,7 @@ class Creator
|
||||
} elseif (is_dir($current_dir . DIRECTORY_SEPARATOR . 'src')) {
|
||||
$replacements[] = '<directory name="src" />';
|
||||
} else {
|
||||
$composer_json_location = $current_dir . DIRECTORY_SEPARATOR . 'composer.json';
|
||||
$composer_json_location = Composer::getJsonFilePath($current_dir);
|
||||
|
||||
if (!file_exists($composer_json_location)) {
|
||||
throw new ConfigCreationException(
|
||||
|
42
src/Psalm/Internal/Composer.php
Normal file
42
src/Psalm/Internal/Composer.php
Normal file
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace Psalm\Internal;
|
||||
|
||||
use function basename;
|
||||
use function getenv;
|
||||
use function pathinfo;
|
||||
use function substr;
|
||||
use function trim;
|
||||
use const PATHINFO_EXTENSION;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
final class Composer
|
||||
{
|
||||
/**
|
||||
* Retrieve the path to composer.json file.
|
||||
*
|
||||
* @see https://github.com/composer/composer/blob/5df1797d20c6ab1eb606dc0f0d76a16ba57ddb7f/src/Composer/Factory.php#L233
|
||||
*/
|
||||
public static function getJsonFilePath(string $root): string
|
||||
{
|
||||
$file_name = getenv('COMPOSER') ?: 'composer.json';
|
||||
$file_name = basename(trim($file_name));
|
||||
|
||||
return $root . '/' . $file_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the path to composer.lock file.
|
||||
*
|
||||
* @see https://github.com/composer/composer/blob/5df1797d20c6ab1eb606dc0f0d76a16ba57ddb7f/src/Composer/Factory.php#L238
|
||||
*/
|
||||
public static function getLockFilePath(string $root): string
|
||||
{
|
||||
$composer_json_path = static::getJsonFilePath($root);
|
||||
return "json" === pathinfo($composer_json_path, PATHINFO_EXTENSION)
|
||||
? substr($composer_json_path, 0, -4).'lock'
|
||||
: $composer_json_path . '.lock';
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
<?php
|
||||
namespace Psalm\Internal\PluginManager;
|
||||
|
||||
use Psalm\Internal\Composer;
|
||||
use function array_filter;
|
||||
use const DIRECTORY_SEPARATOR;
|
||||
use function json_encode;
|
||||
@ -42,13 +43,13 @@ class PluginListFactory
|
||||
if ($this->psalm_root === $this->project_root) {
|
||||
// managing plugins for psalm itself
|
||||
$composer_lock_filenames = [
|
||||
rtrim($this->psalm_root, DIRECTORY_SEPARATOR) . '/composer.lock',
|
||||
Composer::getLockFilePath(rtrim($this->psalm_root, DIRECTORY_SEPARATOR)),
|
||||
];
|
||||
} else {
|
||||
$composer_lock_filenames = [
|
||||
rtrim($this->project_root, DIRECTORY_SEPARATOR) . '/composer.lock',
|
||||
rtrim($this->psalm_root, DIRECTORY_SEPARATOR) . '/../../../composer.lock',
|
||||
rtrim($this->psalm_root, DIRECTORY_SEPARATOR) . '/composer.lock',
|
||||
Composer::getLockFilePath(rtrim($this->project_root, DIRECTORY_SEPARATOR)),
|
||||
Composer::getLockFilePath(rtrim($this->psalm_root, DIRECTORY_SEPARATOR) . '/../../..'),
|
||||
Composer::getLockFilePath(rtrim($this->psalm_root, DIRECTORY_SEPARATOR)),
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -4,12 +4,17 @@ namespace Psalm;
|
||||
|
||||
use Composer\Autoload\ClassLoader;
|
||||
use Phar;
|
||||
use Psalm\Internal\Composer;
|
||||
use function basename;
|
||||
use function dirname;
|
||||
use function getenv;
|
||||
use function pathinfo;
|
||||
use function strpos;
|
||||
use function realpath;
|
||||
use const DIRECTORY_SEPARATOR;
|
||||
use function file_exists;
|
||||
use function in_array;
|
||||
use const PATHINFO_EXTENSION;
|
||||
use const PHP_EOL;
|
||||
use function fwrite;
|
||||
use const STDERR;
|
||||
@ -83,7 +88,8 @@ function requireAutoloaders(string $current_dir, bool $has_explicit_root, string
|
||||
$has_autoloader = true;
|
||||
}
|
||||
|
||||
if (!$has_autoloader && file_exists($autoload_root . '/composer.json')) {
|
||||
$composer_json_file = Composer::getJsonFilePath($autoload_root);
|
||||
if (!$has_autoloader && file_exists($composer_json_file)) {
|
||||
$error_message = 'Could not find any composer autoloaders in ' . $autoload_root;
|
||||
|
||||
if (!$has_explicit_root) {
|
||||
@ -140,7 +146,7 @@ function requireAutoloaders(string $current_dir, bool $has_explicit_root, string
|
||||
*/
|
||||
function getVendorDir(string $current_dir): string
|
||||
{
|
||||
$composer_json_path = $current_dir . DIRECTORY_SEPARATOR . 'composer.json';
|
||||
$composer_json_path = Composer::getJsonFilePath($current_dir);
|
||||
|
||||
if (!file_exists($composer_json_path)) {
|
||||
return 'vendor';
|
||||
|
@ -3,6 +3,7 @@
|
||||
namespace Psalm;
|
||||
|
||||
use Psalm\Internal\Analyzer\ProjectAnalyzer;
|
||||
use Psalm\Internal\Composer;
|
||||
use Psalm\Internal\IncludeCollector;
|
||||
use function gc_disable;
|
||||
use function error_reporting;
|
||||
@ -31,6 +32,7 @@ use function chdir;
|
||||
use function strtolower;
|
||||
|
||||
require_once('command_functions.php');
|
||||
require_once __DIR__ . '/Psalm/Internal/Composer.php';
|
||||
|
||||
gc_disable();
|
||||
|
||||
@ -260,7 +262,7 @@ $providers = new \Psalm\Internal\Provider\Providers(
|
||||
new \Psalm\Internal\Provider\FileStorageCacheProvider($config),
|
||||
new \Psalm\Internal\Provider\ClassLikeStorageCacheProvider($config),
|
||||
new \Psalm\Internal\Provider\FileReferenceCacheProvider($config),
|
||||
new \Psalm\Internal\Provider\ProjectCacheProvider($current_dir . DIRECTORY_SEPARATOR . 'composer.lock')
|
||||
new \Psalm\Internal\Provider\ProjectCacheProvider(Composer::getLockFilePath($current_dir))
|
||||
);
|
||||
|
||||
$project_analyzer = new ProjectAnalyzer(
|
||||
|
@ -3,6 +3,7 @@
|
||||
namespace Psalm;
|
||||
|
||||
require_once('command_functions.php');
|
||||
require_once __DIR__ . '/Psalm/Internal/Composer.php';
|
||||
// show all errors
|
||||
error_reporting(-1);
|
||||
ini_set('display_errors', '1');
|
||||
@ -15,6 +16,7 @@ gc_disable();
|
||||
require_once __DIR__ . '/Psalm/Internal/exception_handler.php';
|
||||
|
||||
use Psalm\Internal\Analyzer\ProjectAnalyzer;
|
||||
use Psalm\Internal\Composer;
|
||||
use Psalm\Internal\IncludeCollector;
|
||||
use Psalm\Progress\DebugProgress;
|
||||
use Psalm\Progress\DefaultProgress;
|
||||
@ -268,7 +270,7 @@ $providers = new \Psalm\Internal\Provider\Providers(
|
||||
new \Psalm\Internal\Provider\FileStorageCacheProvider($config),
|
||||
new \Psalm\Internal\Provider\ClassLikeStorageCacheProvider($config),
|
||||
null,
|
||||
new \Psalm\Internal\Provider\ProjectCacheProvider($current_dir . DIRECTORY_SEPARATOR . 'composer.lock')
|
||||
new \Psalm\Internal\Provider\ProjectCacheProvider(Composer::getLockFilePath($current_dir))
|
||||
);
|
||||
|
||||
$debug = array_key_exists('debug', $options) || array_key_exists('debug-by-line', $options);
|
||||
|
@ -9,10 +9,12 @@ gc_disable();
|
||||
error_reporting(-1);
|
||||
|
||||
require_once('command_functions.php');
|
||||
require_once __DIR__ . '/Psalm/Internal/Composer.php';
|
||||
require_once __DIR__ . '/Psalm/Internal/exception_handler.php';
|
||||
|
||||
use Psalm\Exception\ConfigException;
|
||||
use Psalm\Internal\Analyzer\ProjectAnalyzer;
|
||||
use Psalm\Internal\Composer;
|
||||
use Psalm\Internal\Provider;
|
||||
use Psalm\Internal\IncludeCollector;
|
||||
use Psalm\Progress\DebugProgress;
|
||||
@ -576,7 +578,7 @@ if (isset($options['no-cache']) || isset($options['i'])) {
|
||||
$file_storage_cache_provider,
|
||||
$classlike_storage_cache_provider,
|
||||
new Provider\FileReferenceCacheProvider($config),
|
||||
new Provider\ProjectCacheProvider($current_dir . DIRECTORY_SEPARATOR . 'composer.lock')
|
||||
new Provider\ProjectCacheProvider(Composer::getLockFilePath($current_dir))
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -2,7 +2,9 @@
|
||||
|
||||
namespace Psalm;
|
||||
|
||||
require_once __DIR__ . '/command_functions.php';
|
||||
require_once('command_functions.php');
|
||||
require_once __DIR__ . '/Psalm/Internal/Composer.php';
|
||||
|
||||
use PackageVersions\Versions;
|
||||
use Psalm\Internal\PluginManager\Command\DisableCommand;
|
||||
use Psalm\Internal\PluginManager\Command\EnableCommand;
|
||||
|
@ -3,8 +3,10 @@
|
||||
namespace Psalm;
|
||||
|
||||
require_once('command_functions.php');
|
||||
require_once __DIR__ . '/Psalm/Internal/Composer.php';
|
||||
|
||||
use Psalm\Internal\Analyzer\ProjectAnalyzer;
|
||||
use Psalm\Internal\Composer;
|
||||
use Psalm\Internal\IncludeCollector;
|
||||
use Psalm\Progress\DebugProgress;
|
||||
use Psalm\Progress\DefaultProgress;
|
||||
@ -250,7 +252,7 @@ if (isset($options['no-cache'])) {
|
||||
new \Psalm\Internal\Provider\FileStorageCacheProvider($config),
|
||||
new \Psalm\Internal\Provider\ClassLikeStorageCacheProvider($config),
|
||||
null,
|
||||
new \Psalm\Internal\Provider\ProjectCacheProvider($current_dir . DIRECTORY_SEPARATOR . 'composer.lock')
|
||||
new \Psalm\Internal\Provider\ProjectCacheProvider(Composer::getLockFilePath($current_dir))
|
||||
);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user