Skip to content

Commit

Permalink
NEW Add script for setting phpstan config (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli authored Jan 29, 2024
1 parent 4343640 commit 0b81085
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
38 changes: 38 additions & 0 deletions funcs_scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@

// These functions in scripts can be used in scripts

/**
* Check that a directory exists relative to the root of the module being processed
*
* Example usage:
* check_dir_exists('src')
*/
function check_dir_exists($dirname) {
global $MODULE_DIR;
$path = "$MODULE_DIR/$dirname";
if (!is_dir($path)) {
info("Directory $path does not exist, though this should be OK");
return false;
}
return true;
}

/**
* Check that a file exists relative to the root of the module being processed
*
Expand Down Expand Up @@ -132,6 +148,28 @@ function is_module()
&& strpos($MODULE_DIR, '/webpack-config') === false;
}

/**
* Determine if the module being processed is a theme
*
* Example usage:
* is_theme()
*/
function is_theme()
{
if (!check_file_exists('composer.json')) {
return false;
}

$contents = read_file('composer.json');
$json = json_decode($contents);
if (is_null($json)) {
$lastError = json_last_error();
error("Could not parse from composer.json - last error was $lastError");
}

return $json->type === 'silverstripe-theme';
}

/**
* Determine if the module being processed is a source of documentation
*
Expand Down
50 changes: 50 additions & 0 deletions scripts/cms5/phpstan.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

// Only valid for non-theme modules
if (!is_module() || is_theme()) {
return;
}

// Get the dirs we want to run static analysis against
$dirs = [];
foreach (['code', 'src', 'app/src'] as $codeDir) {
if (check_dir_exists($codeDir)) {
$dirs[] = $codeDir;
}
}

// If we have some dirs to run against, we don't need to do anything.
if (empty($dirs)) {
return;
}

$content = <<<EOT
parameters:
paths:
- %s
EOT;

// Create a phpstan config file
write_file_even_if_exists('phpstan.neon.dist', sprintf($content, implode("\n - ", $dirs)));

// Add composer dependencies.
// composer.json file is already guaranteed to exist - it's used in the is_module()/is_theme() checks above.
// Do not add allow-plugins config - we don't want to be enforcing that for peoples' projects
$contents = read_file('composer.json');
$json = json_decode($contents, true);
$jsonOrig = $json;
if (!$json) {
warning('Failed to parse composer.json');
} else {
if (!array_key_exists('require-dev', $json)) {
$json['require-dev'] = [];
}
$json['require-dev']['silverstripe/standards'] ??= '^1';
$json['require-dev']['phpstan/extension-installer'] ??= '^1.3';

if ($json !== $jsonOrig) {
$flags = JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE;
write_file_even_if_exists('composer.json', json_encode($json, $flags));
}
}

0 comments on commit 0b81085

Please sign in to comment.