Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NEW Add script for setting phpstan config #33

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
emteknetnz marked this conversation as resolved.
Show resolved Hide resolved
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)));
emteknetnz marked this conversation as resolved.
Show resolved Hide resolved

// 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');
emteknetnz marked this conversation as resolved.
Show resolved Hide resolved
} 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));
}
}
Loading