Skip to content

Commit

Permalink
Fully support Psalm 5 behaviour
Browse files Browse the repository at this point in the history
asked by orklah, see
#9112 (comment)
  • Loading branch information
alies-dev committed Jan 16, 2023
1 parent 077cd5d commit 9271946
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/Psalm/Config.php
Expand Up @@ -543,7 +543,7 @@ class Config
* @var string
* @deprecated Please use {@see self::$shepherd_endpoint} instead. Property will be removed in Psalm 6.
*/
public $shepherd_host = 'https://shepherd.dev/hooks/psalm/';
public $shepherd_host = 'shepherd.dev';

/**
* @var string
Expand Down
72 changes: 46 additions & 26 deletions src/Psalm/Internal/Cli/Psalm.php
Expand Up @@ -306,32 +306,7 @@ public static function run(array $argv): void
? $options['find-references-to']
: null;

$is_shepherd_enabled = (bool) ($options['shepherd'] ?? getenv('PSALM_SHEPHERD'));
if ($is_shepherd_enabled) {
$plugins[] = Path::canonicalize(__DIR__ . '/../../Plugin/Shepherd.php');

$custom_shepherd_host = ($options['shepherd'] ?? getenv('PSALM_SHEPHERD')) ?: getenv('PSALM_SHEPHERD_HOST');

$is_valid_custom_shepherd_endpoint = is_string($custom_shepherd_host) && strlen($custom_shepherd_host) > 2;
if ($is_valid_custom_shepherd_endpoint) {
if (parse_url($custom_shepherd_host, PHP_URL_SCHEME) === null) {
$custom_shepherd_host = 'https://' . $custom_shepherd_host;
}

$config->shepherd_host = $custom_shepherd_host;
$config->shepherd_endpoint = $custom_shepherd_host;

if (is_string(getenv('PSALM_SHEPHERD_HOST'))) {
fwrite(
STDERR,
'PSALM_SHEPHERD_HOST env variable is deprecated and will be removed in Psalm 6.'
.' Please use "--shepherd" cli option or PSALM_SHEPHERD env variable'
.' to specify a custom Shepherd host/endpoint.'
. PHP_EOL,
);
}
}
}
self::configureShepherd($options, $plugins);

if (isset($options['clear-cache'])) {
self::clearCache($config);
Expand Down Expand Up @@ -1170,6 +1145,51 @@ private static function configureProjectAnalyzer(
}
}

private static function configureShepherd(Config $config, array $options, array &$plugins): void
{
if (is_string(getenv('PSALM_SHEPHERD_HOST'))) { // remove this block in Psalm 6
fwrite(
STDERR,
'PSALM_SHEPHERD_HOST env variable is deprecated and will be removed in Psalm 6.'
.' Please use "--shepherd" cli option or PSALM_SHEPHERD env variable'
.' to specify a custom Shepherd host/endpoint.'
. PHP_EOL,
);
}

$is_shepherd_enabled = (bool) ($options['shepherd'] ?? getenv('PSALM_SHEPHERD'));
if (! $is_shepherd_enabled) {
return;
}

$plugins[] = Path::canonicalize(__DIR__ . '/../../Plugin/Shepherd.php');

$custom_shepherd_endpoint = $options['shepherd'] ?? getenv('PSALM_SHEPHERD');
if (is_string($custom_shepherd_endpoint) && strlen($custom_shepherd_endpoint) > 2) {
if (parse_url($custom_shepherd_endpoint, PHP_URL_SCHEME) === null) {
$custom_shepherd_endpoint = 'https://' . $custom_shepherd_endpoint;
}

$config->shepherd_endpoint = $custom_shepherd_endpoint;
$config->shepherd_host = str_replace('/hooks/psalm', '', $custom_shepherd_endpoint);

return;
}

// Legacy part, will be removed in Psalm 6
$custom_shepherd_host = getenv('PSALM_SHEPHERD_HOST');

$is_valid_custom_shepherd_endpoint = is_string($custom_shepherd_host);
if ($is_valid_custom_shepherd_endpoint) {
if (parse_url($custom_shepherd_host, PHP_URL_SCHEME) === null) {
$custom_shepherd_host = 'https://' . $custom_shepherd_host;
}

$config->shepherd_endpoint = $custom_shepherd_host . '/hooks/psalm';
$config->shepherd_host = $custom_shepherd_host;
}
}

private static function generateStubs(
array $options,
Providers $providers,
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Plugin/Shepherd.php
Expand Up @@ -81,7 +81,7 @@ public static function afterAnalysis(
$payload = json_encode($data, JSON_THROW_ON_ERROR);

// Prepare new cURL resource
$ch = curl_init($codebase->config->shepherd_endpoint);
$ch = curl_init($base_address . '/hooks/psalm');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
Expand Down

0 comments on commit 9271946

Please sign in to comment.