Skip to content

Commit

Permalink
Merge pull request #9133 from lptn/allow-to-specify-custom-shepherd-e…
Browse files Browse the repository at this point in the history
…ndpoint-clean

Allow to specify custom shepherd endpoint
  • Loading branch information
orklah committed Jan 18, 2023
2 parents dfbd045 + 2161e97 commit a91661a
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 127 deletions.
7 changes: 6 additions & 1 deletion psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="dev-master@8b9cd5fb333866c1e84ca9564394816a7ff5ae6f">
<files psalm-version="dev-master@6bcd3bffe3385bc33643a694c32af515395bf437">
<file src="examples/TemplateChecker.php">
<PossiblyUndefinedIntArrayOffset>
<code>$comment_block-&gt;tags['variablesfrom'][0]</code>
Expand Down Expand Up @@ -483,6 +483,11 @@
<code>$type_tokens[$i - 1]</code>
</PossiblyInvalidArrayOffset>
</file>
<file src="src/Psalm/Plugin/Shepherd.php">
<DeprecatedProperty>
<code>$codebase-&gt;config-&gt;shepherd_host</code>
</DeprecatedProperty>
</file>
<file src="src/Psalm/Storage/ClassConstantStorage.php">
<MutableDependency>
<code>CustomMetadataTrait</code>
Expand Down
11 changes: 10 additions & 1 deletion src/Psalm/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -544,9 +544,18 @@ class Config
*/
public $include_php_versions_in_error_baseline = false;

/** @var string */
/**
* @var string
* @deprecated Please use {@see self::$shepherd_endpoint} instead. Property will be removed in Psalm 6.
*/
public $shepherd_host = 'shepherd.dev';

/**
* @var string
* @internal
*/
public $shepherd_endpoint = 'https://shepherd.dev/hooks/psalm/';

/**
* @var array<string, string>
*/
Expand Down
90 changes: 63 additions & 27 deletions src/Psalm/Internal/Cli/Psalm.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,14 @@
use function json_encode;
use function max;
use function microtime;
use function parse_url;
use function preg_match;
use function preg_replace;
use function realpath;
use function setlocale;
use function str_repeat;
use function str_replace;
use function strlen;
use function strpos;
use function substr;
use function version_compare;
Expand All @@ -77,6 +80,7 @@
use const LC_CTYPE;
use const PHP_EOL;
use const PHP_OS;
use const PHP_URL_SCHEME;
use const PHP_VERSION;
use const STDERR;

Expand Down Expand Up @@ -195,10 +199,9 @@ public static function run(array $argv): void
if (array_key_exists('h', $options)) {
echo self::getHelpText();
/*
--shepherd[=host]
Send data to Shepherd, Psalm's GitHub integration tool.
`host` is the location of the Shepherd server. It defaults to shepherd.dev
More information is available at https://psalm.dev/shepherd
--shepherd[=endpoint]
Send analysis statistics to Shepherd server.
`endpoint` is the URL to the Shepherd server. It defaults to shepherd.dev
*/

exit;
Expand Down Expand Up @@ -279,13 +282,16 @@ public static function run(array $argv): void
chdir($current_dir);
}

/** @var list<string> $plugins List of paths to plugin files */
$plugins = [];

if (isset($options['plugin'])) {
$plugins = $options['plugin'];
$plugins_from_options = $options['plugin'];

if (!is_array($plugins)) {
$plugins = [$plugins];
if (is_array($plugins_from_options)) {
$plugins = $plugins_from_options;
} elseif (is_string($plugins_from_options)) {
$plugins = [$plugins_from_options];
}
}

Expand All @@ -301,24 +307,7 @@ public static function run(array $argv): void
? $options['find-references-to']
: null;

if (isset($options['shepherd']) || getenv('PSALM_SHEPHERD')) {
if (isset($options['shepherd'])) {
if (is_string($options['shepherd'])) {
$config->shepherd_host = $options['shepherd'];
}
} elseif (getenv('PSALM_SHEPHERD')) {
if (false !== ($shepherd_host = getenv('PSALM_SHEPHERD_HOST'))) {
$config->shepherd_host = $shepherd_host;
}
}
$shepherd_plugin = Path::canonicalize(__DIR__ . '/../../Plugin/Shepherd.php');

if (!file_exists($shepherd_plugin)) {
die('Could not find Shepherd plugin location ' . $shepherd_plugin . PHP_EOL);
}

$plugins[] = $shepherd_plugin;
}
self::configureShepherd($config, $options, $plugins);

if (isset($options['clear-cache'])) {
self::clearCache($config);
Expand Down Expand Up @@ -1157,6 +1146,53 @@ 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');

/** @psalm-suppress MixedAssignment */
$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;
}

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

return;
}

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

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

/** @psalm-suppress DeprecatedProperty */
$config->shepherd_host = $custom_shepherd_host;
$config->shepherd_endpoint = $custom_shepherd_host . '/hooks/psalm';
}
}

private static function generateStubs(
array $options,
Providers $providers,
Expand Down Expand Up @@ -1329,8 +1365,8 @@ private static function getHelpText(): string
--generate-stubs=PATH
Generate stubs for the project and dump the file in the given path
--shepherd[=host]
Send data to Shepherd, Psalm’s GitHub integration tool.
--shepherd[=endpoint]
Send analysis statistics to Shepherd (shepherd.dev) or your server.
--alter
Run Psalter
Expand Down

0 comments on commit a91661a

Please sign in to comment.