Commit 46eb9b25 authored by herczogzoltan's avatar herczogzoltan
Browse files

Add composer dependencies, add code analyser service and form to trigger code analyser

parent f516ba3c
{
"name": "drupal/upgrade_status",
"type": "drupal-module",
"description": "Tool to review deprecation status.",
"homepage": "http://drupal.org/project/upgrade_status",
"license": "GPL-2.0-or-later",
"require": {
"drupal/core": "^8.6",
"mglaman/phpstan-drupal": "^0.12.0@dev",
"mglaman/phpstan-drupal-deprecations": "^0.11.1",
"mglaman/phpstan-junit": "^0.11.1",
"phpstan/phpstan-deprecation-rules": "^0.11.0@dev"
},
"minimum-stability": "dev"
}
parameters:
excludes_analyse:
- *.api.php
- */tests/Drupal/Tests/Listeners/Legacy/*
- */tests/fixtures/*.php
customRulesetUsed: true
# Ignore phpstan-drupal extension's rules.
fileExtensions:
- module
- theme
- inc
ignoreErrors:
- '#\Drupal calls should be avoided in classes, use dependency injection instead#'
- '#Plugin definitions cannot be altered.#'
- '#Missing cache backend declaration for performance.#'
- '#Plugin manager has cache backend specified but does not declare cache tags.#'
reportUnmatchedIgnoredErrors: false
# Ignore tests
extensions:
drupal: PHPStan\DependencyInjection\DrupalExtension
drupal:
entityTypeStorageMapping:
node: Drupal\node\NodeStorage
taxonomy_term: Drupal\taxonomy\TermStorage
user: Drupal\user\UserStorage
rules:
- PHPStan\Rules\Classes\PluginManagerInspectionRule
- PHPStan\Rules\Drupal\Coder\DiscouragedFunctionsRule
- PHPStan\Rules\Drupal\GlobalDrupalDependencyInjectionRule
- PHPStan\Rules\Drupal\PluginManager\PluginManagerSetsCacheBackendRule
- PHPStan\Rules\Deprecations\AccessDeprecatedPropertyRule
- PHPStan\Rules\Deprecations\AccessDeprecatedStaticPropertyRule
- PHPStan\Rules\Deprecations\CallToDeprecatedFunctionRule
- PHPStan\Rules\Deprecations\CallToDeprecatedMethodRule
- PHPStan\Rules\Deprecations\CallToDeprecatedStaticMethodRule
- PHPStan\Rules\Deprecations\FetchingClassConstOfDeprecatedClassRule
- PHPStan\Rules\Deprecations\ImplementationOfDeprecatedInterfaceRule
- PHPStan\Rules\Deprecations\InheritanceOfDeprecatedClassRule
- PHPStan\Rules\Deprecations\InheritanceOfDeprecatedInterfaceRule
- PHPStan\Rules\Deprecations\InstantiationOfDeprecatedClassRule
- PHPStan\Rules\Deprecations\UsageOfDeprecatedTraitRule
services:
drupal.serviceMapFactory:
class: PHPStan\Drupal\ServiceMapFactoryInterface
factory: PHPStan\Drupal\ServiceMapFactory(%drupalServiceMap%)
-
class: @drupal.serviceMapFactory::create()
-
class: PHPStan\Type\EntityTypeManagerGetStorageDynamicReturnTypeExtension
arguments:
entityTypeStorageMapping: %drupal.entityTypeStorageMapping%
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
-
class: PHPStan\Type\ServiceDynamicReturnTypeExtension
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
errorFormatter.junit:
class: PHPStan\Command\ErrorFormatter\JUnitErrorFormatter
<?php
namespace Drupal\upgrade_status;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use PHPStan\Command\AnalyseApplication;
use PHPStan\Command\CommandHelper;
use PHPStan\Command\ErrorFormatter\ErrorFormatter;
use PHPStan\Command\InceptionNotSuccessfulException;
use PHPStan\ShouldNotHappenException;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\BufferedOutput;
class DeprecationAnalyser {
use StringTranslationTrait;
const ERROR_FORMAT = 'json';
/**
* @var \Drupal\Core\Cache\CacheBackendInterface
*/
private $cache;
/**
* @var \Drupal\Core\Logger\LoggerChannelInterface
*/
private $logger;
/**
* DeprecationAnalyser constructor.
*
* @param \Drupal\Core\Cache\CacheBackendInterface $cache
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $loggerFactory
*/
public function __construct(
CacheBackendInterface $cache,
LoggerChannelFactoryInterface $loggerFactory
) {
$this->cache = $cache;
$this->logger = $loggerFactory->get('readiness');
}
public function analyse() {
$modulePath = drupal_get_path('module', 'upgrade_status');
if (!isset($GLOBALS['autoloaderInWorkingDirectory'])) {
$GLOBALS['autoloaderInWorkingDirectory'] = implode(DIRECTORY_SEPARATOR, [DRUPAL_ROOT, 'autoload.php']);
}
$errorOutput = new BufferedOutput();
$configuration = implode(DIRECTORY_SEPARATOR, [DRUPAL_ROOT, $modulePath, 'deprecation_testing.neon']);
$files = [];
$files[] = implode(DIRECTORY_SEPARATOR, [DRUPAL_ROOT, 'core/lib/Drupal.php']);
try {
$inspectionResult = CommandHelper::begin(
new StringInput('analyse'),
$errorOutput,
$files,
null,
null,
null,
$configuration,
null
);
} catch (InceptionNotSuccessfulException $e) {
$this->logger->error($e);
} catch (ShouldNotHappenException $e) {
$this->logger->error($e);
}
$container = $inspectionResult->getContainer();
$errorFormatterServiceName = sprintf('errorFormatter.%s', self::ERROR_FORMAT);
if (!$container->hasService($errorFormatterServiceName)) {
$this->logger->error($this->t('Error formatter @formatter not found'), ['@formatter' => self::ERROR_FORMAT]);
return '';
}
/** @var ErrorFormatter $errorFormatter */
$errorFormatter = $container->getService($errorFormatterServiceName);
$application = $container->getByType(AnalyseApplication::class);
$inspectionResult->handleReturn(
$application->analyse(
$inspectionResult->getFiles(),
$inspectionResult->isOnlyFiles(),
$inspectionResult->getConsoleStyle(),
$errorFormatter,
$inspectionResult->isDefaultLevelUsed(),
FALSE
)
);
return $errorOutput->fetch();
}
}
<?php
interface DeprecationAnalyserInterface {
public function analyse(): array;
}
<?php
namespace Drupal\upgrade_status\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\upgrade_status\DeprecationAnalyser;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ReadinessForm extends FormBase {
/**
* @var \Drupal\upgrade_status\DeprecationAnalyser
*/
protected $deprecationAnalyser;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('upgrade_status.deprecation_analyser')
);
}
/**
* ReadinessForm constructor.
*
* @param \Drupal\upgrade_status\DeprecationAnalyser $deprecationAnalyser
*/
public function __construct(DeprecationAnalyser $deprecationAnalyser) {
$this->deprecationAnalyser = $deprecationAnalyser;
}
/**
* Returns a unique string identifying the form.
*
* The returned ID should be a unique string that can be a valid PHP function
* name, since it's used in hook implementation names such as
* hook_form_FORM_ID_alter().
*
* @return string
* The unique string identifying the form.
*/
public function getFormId() {
return 'drupal_readiness_form';
}
/**
* Form constructor.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return array
* The form structure.
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$data = $form_state->get('deprecation_data');
$form['drupal_readiness_form'] = [
'response' => [
'#type' => 'details',
'#title' => $this->t('Response'),
'#tree' => TRUE,
'#open' => TRUE,
'data' => [
'#type' => '#markup',
'#markup' => $data,
],
],
];
$form['drupal_readiness_form']['action']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Analyse'),
];
return $form;
}
/**
* Form submission handler.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$form_state->setRebuild(TRUE);
$form_state->set('deprecation_data', $this->deprecationAnalyser->analyse());
}
}
upgrade_status.analyse:
path: '/admin/reports/updates/deprecations'
defaults:
_title: 'Drupal 9 Readiness'
_form: 'Drupal\upgrade_status\Form\ReadinessForm'
requirements:
_permission: 'administer site configuration'
services:
upgrade_status.deprecation_analyser:
class: Drupal\upgrade_status\DeprecationAnalyser
arguments: ['@cache.default', '@logger.factory']
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment