View source
<?php
namespace Drupal\upgrade_status;
use Composer\Semver\Semver;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\Extension;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Exception;
use GuzzleHttp\Client;
use Nette\Neon\Neon;
use PHPStan\Command\AnalyseApplication;
use PHPStan\Command\CommandHelper;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\BufferedOutput;
class DeprecationAnalyser implements DeprecationAnalyserInterface {
use StringTranslationTrait;
const CORE_MINOR_OLDEST_SUPPORTED = '8.7';
const ERROR_FORMAT = 'json';
protected $scanResultStorage;
protected $logger;
protected $inputInterface;
protected $outputInterface;
protected $phpstanNeonPath;
protected $upgradeStatusTemporaryDirectory;
protected $config;
protected $httpClient;
protected $fileSystem;
public function __construct(KeyValueFactoryInterface $key_value_factory, LoggerInterface $logger, StringInput $input, BufferedOutput $output, ConfigFactoryInterface $config_factory, Client $http_client, FileSystemInterface $file_system) {
$this->scanResultStorage = $key_value_factory
->get('upgrade_status_scan_results');
$this->logger = $logger;
$this->inputInterface = $input;
$this->outputInterface = $output;
$this->config = $config_factory
->get('upgrade_status.settings');
$this->httpClient = $http_client;
$this->fileSystem = $file_system;
$this
->populateAutoLoader();
$this->upgradeStatusTemporaryDirectory = file_directory_temp() . '/upgrade_status';
$this->phpstanNeonPath = $this->upgradeStatusTemporaryDirectory . '/deprecation_testing.neon';
if (!file_exists($this->phpstanNeonPath)) {
$this
->prepareTempDirectory();
$this
->createModifiedNeonFile();
}
}
protected function populateAutoLoader() {
require_once DRUPAL_ROOT . '/core/tests/bootstrap.php';
drupal_phpunit_populate_class_loader();
}
public function analyse(Extension $extension) {
drupal_register_shutdown_function([
$this,
'logFatalError',
], $extension);
if (!isset($GLOBALS['autoloaderInWorkingDirectory'])) {
$GLOBALS['autoloaderInWorkingDirectory'] = DRUPAL_ROOT . '/autoload.php';
}
$project_dir = DRUPAL_ROOT . '/' . $extension->subpath;
$paths = $this
->getDirContents($project_dir);
foreach ($paths as $key => $file_path) {
if (substr($file_path, -3) !== 'php' && substr($file_path, -7) !== '.module' && substr($file_path, -8) !== '.install' && substr($file_path, -3) !== 'inc') {
unset($paths[$key]);
}
}
$this->logger
->notice($this
->t("Extension @project_machine_name contains @number files to process.", [
'@project_machine_name' => $extension
->getName(),
'@number' => count($paths),
]));
$result = [];
$result['date'] = REQUEST_TIME;
$result['data'] = [
'totals' => [
'errors' => 0,
'file_errors' => 0,
],
'files' => [],
];
$info = $extension->info;
if (!isset($info['core_version_requirement'])) {
$result['data']['files'][$extension
->getFilename()]['messages'] = [
[
'message' => 'Add <code>core_version_requirement: ^8 || ^9</code> to ' . $extension
->getFilename() . ' to designate that the module is compatible with Drupal 9. See https://www.drupal.org/node/3070687.',
'line' => 0,
],
];
$result['data']['totals']['errors']++;
$result['data']['totals']['file_errors']++;
}
elseif (!Semver::satisfies('9.0.0', $info['core_version_requirement'])) {
$result['data']['files'][$extension
->getFilename()]['messages'] = [
[
'message' => "The current value <code>core_version_requirement: {$info['core_version_requirement']}</code> in {$extension->getFilename()} is not compatible with Drupal 9.0.0. See https://www.drupal.org/node/3070687.",
'line' => 0,
],
];
$result['data']['totals']['errors']++;
$result['data']['totals']['file_errors']++;
}
if (!empty($paths)) {
$num_of_files = $this->config
->get('paths_per_scan') ?: 30;
for ($offset = 0; $offset <= count($paths); $offset += $num_of_files) {
$files = array_slice($paths, $offset, $num_of_files);
if (!empty($files)) {
$raw_errors = $this
->runPhpStan($files);
$errors = json_decode($raw_errors, TRUE);
if (!is_array($errors)) {
continue;
}
$result['data']['totals']['errors'] += $errors['totals']['errors'];
$result['data']['totals']['file_errors'] += $errors['totals']['file_errors'];
$result['data']['files'] = array_merge($result['data']['files'], $errors['files']);
}
}
}
foreach ($result['data']['files'] as $path => &$errors) {
if (!empty($errors['messages'])) {
foreach ($errors['messages'] as &$error) {
list($message, $category) = $this
->categorizeMessage($error['message'], $extension);
$error['message'] = $message;
$error['upgrade_status_category'] = $category;
@$result['data']['totals']['upgrade_status_category'][$category]++;
if (in_array($category, [
'safe',
'old',
])) {
@$result['data']['totals']['upgrade_status_split']['error']++;
}
elseif (in_array($category, [
'later',
'uncategorized',
])) {
@$result['data']['totals']['upgrade_status_split']['warning']++;
}
}
}
}
if (!empty($extension->info['project'])) {
$response = $this->httpClient
->request('GET', 'https://www.drupal.org/api-d7/node.json?field_project_machine_name=' . $extension
->getName());
if ($response
->getStatusCode()) {
$data = json_decode($response
->getBody(), TRUE);
if (!empty($data['list'][0]['field_next_major_version_info']['value'])) {
$result['plans'] = str_replace('href="/', 'href="https://drupal.org/', $data['list'][0]['field_next_major_version_info']['value']);
}
}
}
$this->scanResultStorage
->set($extension
->getName(), json_encode($result));
}
public function getDirContents(string $dir) {
$results = [];
$files = scandir($dir);
foreach ($files as $value) {
$path = realpath($dir . '/' . $value);
if (!is_dir($path)) {
$results[] = $path;
continue;
}
if ($value != '.' && $value != '..') {
$results = array_merge($results, $this
->getDirContents($path, $results));
}
}
return $results;
}
public function runPhpStan(array $paths) {
try {
$result = CommandHelper::begin($this->inputInterface, $this->outputInterface, $paths, NULL, NULL, NULL, $this->phpstanNeonPath, NULL, FALSE);
} catch (Exception $e) {
$this->logger
->error($e);
}
$container = $result
->getContainer();
$error_formatter_service = sprintf('errorFormatter.%s', self::ERROR_FORMAT);
if (!$container
->hasService($error_formatter_service)) {
$this->logger
->error('Error formatter @formatter not found.', [
'@formatter' => self::ERROR_FORMAT,
]);
}
else {
$errorFormatter = $container
->getService($error_formatter_service);
$application = $container
->getByType(AnalyseApplication::class);
$result
->handleReturn($application
->analyse($result
->getFiles(), $result
->isOnlyFiles(), $result
->getConsoleStyle(), $errorFormatter, $result
->isDefaultLevelUsed(), FALSE, NULL));
return $this->outputInterface
->fetch();
}
}
protected function prepareTempDirectory() {
$success = $this->fileSystem
->prepareDirectory($this->upgradeStatusTemporaryDirectory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
if (!$success) {
$this->logger
->error($this
->t("Unable to create temporary directory for Upgrade Status: @directory.", [
'@directory' => $this->upgradeStatusTemporaryDirectory,
]));
return $success;
}
$phpstan_cache_directory = $this->upgradeStatusTemporaryDirectory . '/phpstan';
$success = $this->fileSystem
->prepareDirectory($phpstan_cache_directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
if (!$success) {
$this->logger
->error($this
->t("Unable to create temporary directory for PHPStan: @directory.", [
'@directory' => $phpstan_cache_directory,
]));
}
return $success;
}
protected function createModifiedNeonFile() {
$module_path = drupal_get_path('module', 'upgrade_status');
$unmodified_neon_file = DRUPAL_ROOT . "/{$module_path}/deprecation_testing.neon";
$config = file_get_contents($unmodified_neon_file);
$neon = Neon::decode($config);
$neon['parameters']['tmpDir'] = $this->upgradeStatusTemporaryDirectory . '/phpstan';
$success = file_put_contents($this->phpstanNeonPath, Neon::encode($neon), Neon::BLOCK);
if (!$success) {
$this->logger
->error($this
->t("Couldn't write configuration for PHPStan: @file.", [
'@file' => $this->phpstanNeonPath,
]));
}
return $success ? TRUE : FALSE;
}
public function logFatalError(Extension $extension) {
$project_name = $extension
->getName();
$result = $this->scanResultStorage
->get($project_name);
$message = error_get_last();
if (empty($result)) {
$this->logger
->error($this
->t("Fatal error occurred for @project_machine_name.", [
'@project_machine_name' => $project_name,
]));
$result = [];
$result['date'] = REQUEST_TIME;
$result['data'] = [
'totals' => [
'errors' => 0,
'file_errors' => 1,
],
'files' => [],
];
$file_name = $message['file'];
$result['data']['files'][$file_name] = [
'errors' => 1,
'messages' => [
[
'message' => $message['message'],
'line' => $message['line'],
],
],
];
$this->scanResultStorage
->set($project_name, json_encode($result));
}
}
protected function categorizeMessage(string $error, Extension $extension) {
$error = preg_replace('!:\\s+(in|as of)!', '. Deprecated \\1', $error);
$version = '';
if (preg_match('!\\\\(Web|)TestBase. Deprecated in [Dd]rupal[ :]8.8.0 !', $error)) {
$version = '8.6.0';
$error .= " Replacement available from drupal:8.6.0.";
}
elseif (preg_match('!Deprecated (in|as of) [Dd]rupal[ :](8.\\d)!', $error, $version_found)) {
$version = $version_found[2];
}
$category = 'uncategorized';
if (!empty($version)) {
if (!empty($extension->info['project'])) {
if (version_compare($version, self::CORE_MINOR_OLDEST_SUPPORTED) <= 0) {
$category = 'old';
}
else {
$category = 'later';
}
}
else {
if (version_compare($version, \Drupal::VERSION) <= 0) {
$category = 'safe';
}
else {
$category = 'later';
}
}
}
if (preg_match('!(will be|is) removed (before|from) [Dd]rupal[ :](10.\\d)!', $error)) {
$category = 'ignore';
}
return [
$error,
$category,
];
}
}