Verified Commit 64fb3ff5 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3438846 by mondrake: Add return typehints for classes that inherit from Composer

parent 28d407eb
Loading
Loading
Loading
Loading
Loading
+20 −18
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Composer\Plugin\VendorHardening;

use Composer\Composer;
@@ -58,7 +60,7 @@ class VendorHardeningPlugin implements PluginInterface, EventSubscriberInterface
  /**
   * {@inheritdoc}
   */
  public function activate(Composer $composer, IOInterface $io) {
  public function activate(Composer $composer, IOInterface $io): void {
    $this->composer = $composer;
    $this->io = $io;

@@ -69,19 +71,19 @@ public function activate(Composer $composer, IOInterface $io) {
  /**
   * {@inheritdoc}
   */
  public function deactivate(Composer $composer, IOInterface $io) {
  public function deactivate(Composer $composer, IOInterface $io): void {
  }

  /**
   * {@inheritdoc}
   */
  public function uninstall(Composer $composer, IOInterface $io) {
  public function uninstall(Composer $composer, IOInterface $io): void {
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
  public static function getSubscribedEvents(): array {
    return [
      ScriptEvents::POST_AUTOLOAD_DUMP => 'onPostAutoloadDump',
      ScriptEvents::POST_UPDATE_CMD => 'onPostCmd',
@@ -99,7 +101,7 @@ public static function getSubscribedEvents() {
   * @param \Composer\Script\Event $event
   *   The Composer event.
   */
  public function onPostAutoloadDump(Event $event) {
  public function onPostAutoloadDump(Event $event): void {
    $this->writeAccessRestrictionFiles($this->composer->getConfig()->get('vendor-dir'));
  }

@@ -109,7 +111,7 @@ public function onPostAutoloadDump(Event $event) {
   * @param \Composer\Script\Event $event
   *   The Composer event.
   */
  public function onPostCmd(Event $event) {
  public function onPostCmd(Event $event): void {
    $this->cleanAllPackages();
  }

@@ -119,7 +121,7 @@ public function onPostCmd(Event $event) {
   * @param \Composer\Installer\PackageEvent $event
   *   The package event.
   */
  public function onPrePackageInstall(PackageEvent $event) {
  public function onPrePackageInstall(PackageEvent $event): void {
    /** @var \Composer\Package\CompletePackage $package */
    $package = $event->getOperation()->getPackage();
    $this->removeBinBeforeCleanup($package);
@@ -131,7 +133,7 @@ public function onPrePackageInstall(PackageEvent $event) {
   * @param \Composer\Installer\PackageEvent $event
   *   The package event.
   */
  public function onPrePackageUpdate(PackageEvent $event) {
  public function onPrePackageUpdate(PackageEvent $event): void {
    /** @var \Composer\Package\CompletePackage $package */
    $package = $event->getOperation()->getTargetPackage();
    $this->removeBinBeforeCleanup($package);
@@ -143,7 +145,7 @@ public function onPrePackageUpdate(PackageEvent $event) {
   * @param \Composer\Installer\PackageEvent $event
   *   The package event.
   */
  public function onPostPackageInstall(PackageEvent $event) {
  public function onPostPackageInstall(PackageEvent $event): void {
    $this->cleanPackage($event->getOperation()->getPackage());
  }

@@ -153,7 +155,7 @@ public function onPostPackageInstall(PackageEvent $event) {
   * @param \Composer\Installer\PackageEvent $event
   *   The package event.
   */
  public function onPostPackageUpdate(PackageEvent $event) {
  public function onPostPackageUpdate(PackageEvent $event): void {
    $this->cleanPackage($event->getOperation()->getTargetPackage());
  }

@@ -166,7 +168,7 @@ public function onPostPackageUpdate(PackageEvent $event) {
   * @param \Composer\Package\BasePackage $package
   *   The package we're cleaning up.
   */
  protected function removeBinBeforeCleanup(BasePackage $package) {
  protected function removeBinBeforeCleanup(BasePackage $package): void {
    // We can process AliasPackage and Package objects, and they share the
    // BasePackage parent class. However, since there is no common interface for
    // these package types that allow for the setBinaries() method, and since
@@ -208,7 +210,7 @@ protected function removeBinBeforeCleanup(BasePackage $package) {
   * @return string[]
   *   Bin files to remove, with the file as both the key and the value.
   */
  protected function findBinOverlap($binaries, $clean_paths) {
  protected function findBinOverlap(array $binaries, array $clean_paths): array {
    // Make a filesystem model to explore. This is a keyed array that looks like
    // all the places that will be removed by cleanup. 'tests/src' becomes
    // $filesystem['tests']['src'] = TRUE;
@@ -250,7 +252,7 @@ protected function findBinOverlap($binaries, $clean_paths) {
   * @return \Composer\Package\PackageInterface[]
   *   The list of installed packages.
   */
  protected function getInstalledPackages() {
  protected function getInstalledPackages(): array {
    return $this->composer->getRepositoryManager()->getLocalRepository()->getPackages();
  }

@@ -264,7 +266,7 @@ protected function getInstalledPackages() {
   *   Path to the install path for the package, relative to the project. This
   *   accounts for changes made by composer/installers, if any.
   */
  protected function getInstallPathForPackage(PackageInterface $package) {
  protected function getInstallPathForPackage(PackageInterface $package): string {
    return $this->composer->getInstallationManager()->getInstallPath($package);
  }

@@ -273,7 +275,7 @@ protected function getInstallPathForPackage(PackageInterface $package) {
   *
   * This applies in the context of a post-command event.
   */
  public function cleanAllPackages() {
  public function cleanAllPackages(): void {
    // Get a list of all the packages available after the update or install
    // command.
    $installed_packages = [];
@@ -309,7 +311,7 @@ public function cleanAllPackages() {
   * @param \Composer\Package\PackageInterface $package
   *   The package to clean.
   */
  public function cleanPackage(PackageInterface $package) {
  public function cleanPackage(PackageInterface $package): void {
    // Normalize package names to lower case.
    $package_name = strtolower($package->getName());
    if (isset($this->packagesAlreadyCleaned[$package_name])) {
@@ -332,7 +334,7 @@ public function cleanPackage(PackageInterface $package) {
   * @param string $paths_for_package
   *   List of directories in $package_name to remove
   */
  protected function cleanPathsForPackage(PackageInterface $package, $paths_for_package) {
  protected function cleanPathsForPackage(PackageInterface $package, $paths_for_package): void {
    // Whatever happens here, this package counts as cleaned so that we don't
    // process it more than once.
    $package_name = strtolower($package->getName());
@@ -373,7 +375,7 @@ protected function cleanPathsForPackage(PackageInterface $package, $paths_for_pa
   * @param string $vendor_dir
   *   Path to vendor directory.
   */
  public function writeAccessRestrictionFiles($vendor_dir) {
  public function writeAccessRestrictionFiles(string $vendor_dir): void {
    $this->io->writeError('<info>Hardening vendor directory with .htaccess and web.config files.</info>');
    // Prevent access to vendor directory on Apache servers.
    FileSecurity::writeHtaccess($vendor_dir, TRUE);
+0 −2
Original line number Diff line number Diff line
@@ -7,8 +7,6 @@

# Skip some dependencies' DebugClassLoader forward compatibility warnings.
%Method "Behat\\[^"]+" might add "[^"]+" as a native return type declaration in the future. Do the same in (child class|implementation) "[^"]+" now to avoid errors or add an explicit @return annotation to suppress this message%
%Method "Composer\\EventDispatcher\\EventSubscriberInterface::getSubscribedEvents\(\)" might add "[^"]+" as a native return type declaration in the future. Do the same in (child class|implementation) "[^"]+" now to avoid errors or add an explicit @return annotation to suppress this message%
%Method "Composer\\Plugin\\PluginInterface::(activate|deactivate|uninstall)\(\)" might add "[^"]+" as a native return type declaration in the future. Do the same in (child class|implementation) "[^"]+" now to avoid errors or add an explicit @return annotation to suppress this message%
%Method "Doctrine\\Common\\Annotations\\Reader::[^"]+" might add "[^"]+" as a native return type declaration in the future. Do the same in (child class|implementation) "[^"]+" now to avoid errors or add an explicit @return annotation to suppress this message%
%Method "Twig\\Extension\\ExtensionInterface::[^"]+" might add "[^"]+" as a native return type declaration in the future. Do the same in (child class|implementation) "[^"]+" now to avoid errors or add an explicit @return annotation to suppress this message%
%Method "Twig\\Loader\\FilesystemLoader::findTemplate\(\)" might add "[^"]+" as a native return type declaration in the future. Do the same in (child class|implementation) "[^"]+" now to avoid errors or add an explicit @return annotation to suppress this message%