Skip to content
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
2 changes: 1 addition & 1 deletion phpcs.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<ruleset name="phpstan-drupal">
<config name="php_version" value="70400"/>
<config name="php_version" value="80100"/>
<arg name="colors"/>
<arg name="extensions" value="php"/>
<arg name="encoding" value="utf-8"/>
Expand Down
29 changes: 14 additions & 15 deletions src/Drupal/DrupalAutoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,52 +32,51 @@
use function ucwords;
use function usort;

/**
* Bootstraps a Drupal site for analysis from PHPStan's bootstrap file.
*
* @internal
*/
class DrupalAutoloader
{

/**
* @var \Composer\Autoload\ClassLoader
*/
private $autoloader;
private ClassLoader $autoloader;

/**
* @var string
*/
private $drupalRoot;
private string $drupalRoot;

/**
* List of available modules.
*
* @var Extension[]
*/
protected $moduleData = [];
protected array $moduleData = [];

/**
* List of available themes.
*
* @var Extension[]
*/
protected $themeData = [];
protected array $themeData = [];

/**
* @var array<array<string, string>>
*/
private $serviceMap = [];
private array $serviceMap = [];

/**
* @var array<string, string>
*/
private $serviceYamls = [];
private array $serviceYamls = [];

/**
* @var array<string, string>
*/
private $serviceClassProviders = [];
private array $serviceClassProviders = [];

/**
* @var array
* @var array<string, mixed>
*/
private $namespaces = [];
private array $namespaces = [];

public function register(Container $container): void
{
Expand Down
65 changes: 12 additions & 53 deletions src/Drupal/DrupalServiceDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,52 +11,23 @@
class DrupalServiceDefinition
{

/**
* @var string
*/
private $id;

/**
* @var string|null
*/
private $class;

/**
* @var bool
*/
private $public;

/**
* @var bool
*/
private $deprecated = false;

/**
* @var string|null
*/
private $deprecationTemplate;
private const DEFAULT_DEPRECATION_TEMPLATE = 'The "%service_id%" service is deprecated. You should stop using it, as it will soon be removed.';

/**
* @var string
*/
private static $defaultDeprecationTemplate = 'The "%service_id%" service is deprecated. You should stop using it, as it will soon be removed.';
private bool $deprecated = false;

/**
* @var string|null
*/
private $alias;
private ?string $deprecationTemplate = null;

/**
* @var array<string, \mglaman\PHPStanDrupal\Drupal\DrupalServiceDefinition>
*/
private $decorators = [];

public function __construct(string $id, ?string $class, bool $public = true, ?string $alias = null)
{
$this->id = $id;
$this->class = $class;
$this->public = $public;
$this->alias = $alias;
private array $decorators = [];

public function __construct(
private readonly string $id,
private readonly ?string $class,
private readonly bool $public = true,
private readonly ?string $alias = null
) {
}

public function setDeprecated(bool $status = true, ?string $template = null): void
Expand All @@ -65,33 +36,21 @@ public function setDeprecated(bool $status = true, ?string $template = null): vo
$this->deprecationTemplate = $template;
}

/**
* @return string
*/
public function getId(): string
{
return $this->id;
}

/**
* @return string|null
*/
public function getClass(): ?string
{
return $this->class;
}

/**
* @return bool
*/
public function isPublic(): bool
{
return $this->public;
}

/**
* @return string|null
*/
public function getAlias(): ?string
{
return $this->alias;
Expand All @@ -104,7 +63,7 @@ public function isDeprecated(): bool

public function getDeprecatedDescription(): string
{
return str_replace('%service_id%', $this->id, $this->deprecationTemplate ?? self::$defaultDeprecationTemplate);
return str_replace('%service_id%', $this->id, $this->deprecationTemplate ?? self::DEFAULT_DEPRECATION_TEMPLATE);
}

public function getType(): Type
Expand Down
79 changes: 24 additions & 55 deletions src/Drupal/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,74 +8,38 @@
use function file_get_contents;
use function is_array;
use function sprintf;
use function strpos;
use function str_contains;
use function trim;

/**
* Defines an extension (file) object.
*
* Bundled version of \Drupal\Core\Extension\Extension.
*
* @internal
*/
class Extension
{

/**
* The type of the extension (e.g., 'module').
*
* @var string
* The subpath of the extension below the search path it was found in.
*/
protected $type;
public string $subpath = '';

/**
* The relative pathname of the extension (e.g.,
* 'core/modules/node/node.info.yml').
*
* @var string
*/
protected $pathname;

/**
* The filename of the main extension file (e.g., 'node.module').
*
* @var string|null
* The originating search path directory (e.g., 'core').
*/
protected $filename;
public string $origin = '';

/**
* An SplFileInfo instance for the extension's info file.
*
* Note that SplFileInfo is a PHP resource and resources cannot be serialized.
*
* @var ?\SplFileInfo
*/
protected $splFileInfo;

/**
* The app root.
*
* @var string
* @var array<mixed>|null
*/
protected $root;

/**
* @var string
*/
public $subpath = '';

/**
* @var string
*/
public $origin = '';

/**
* @var array|null
*/
private $info;
private ?array $info = null;

/**
* @var string[]|null
*/
private $dependencies;
private ?array $dependencies = null;

/**
* Constructs a new Extension object.
Expand All @@ -87,15 +51,15 @@ class Extension
* @param string $pathname
* The relative path and filename of the extension's info file; e.g.,
* 'core/modules/node/node.info.yml'.
* @param string $filename
* @param string|null $filename
* (optional) The filename of the main extension file; e.g., 'node.module'.
*/
public function __construct($root, $type, $pathname, $filename = null)
{
$this->root = $root;
$this->type = $type;
$this->pathname = $pathname;
$this->filename = $filename;
public function __construct(
protected string $root,
protected string $type,
protected string $pathname,
protected ?string $filename = null
) {
}

/**
Expand Down Expand Up @@ -212,7 +176,7 @@ public function getDependencies(): array

// @see \Drupal\Core\Extension\Dependency::createFromString().
foreach ($dependencies as $dependency) {
if (strpos($dependency, ':') !== false) {
if (str_contains($dependency, ':')) {
[, $dependency] = explode(':', $dependency);
}

Expand All @@ -234,6 +198,11 @@ private function parseInfo(): array
throw new RuntimeException(sprintf('Cannot read "%s"', $this->getPathname()));
}

return $this->info = Yaml::parse($infoContent);
$parsed = Yaml::parse($infoContent);
if (!is_array($parsed)) {
throw new RuntimeException(sprintf('Malformed info file "%s"', $this->getPathname()));
}

return $this->info = $parsed;
}
}
Loading
Loading