Completed
Push — fix/autoloader-reuse-id ( 49c8fc )
by
unknown
13:07 queued 03:52
created

CustomAutoloaderPlugin   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 135
Duplicated Lines 10.37 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 14
loc 135
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A activate() 0 4 1
A deactivate() 0 6 1
A uninstall() 0 6 1
A getSubscribedEvents() 0 5 1
A postAutoloadDump() 0 22 2
B determineSuffix() 14 31 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php //phpcs:ignore WordPress.Files.FileName.NotHyphenatedLowercase
2
/**
3
 * Custom Autoloader Composer Plugin, hooks into composer events to generate the custom autoloader.
4
 *
5
 * @package automattic/jetpack-autoloader
6
 */
7
8
// phpcs:disable PHPCompatibility.Keywords.NewKeywords.t_useFound
9
// phpcs:disable PHPCompatibility.LanguageConstructs.NewLanguageConstructs.t_ns_separatorFound
10
// phpcs:disable PHPCompatibility.Keywords.NewKeywords.t_namespaceFound
11
// phpcs:disable WordPress.Files.FileName.NotHyphenatedLowercase
12
// phpcs:disable WordPress.Files.FileName.InvalidClassFileName
13
// phpcs:disable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
14
15
namespace Automattic\Jetpack\Autoloader;
16
17
use Composer\Composer;
18
use Composer\IO\IOInterface;
19
use Composer\Script\Event;
20
use Composer\Script\ScriptEvents;
21
use Composer\Plugin\PluginInterface;
22
use Composer\EventDispatcher\EventSubscriberInterface;
23
24
/**
25
 * Class CustomAutoloaderPlugin.
26
 *
27
 * @package automattic/jetpack-autoloader
28
 */
29
class CustomAutoloaderPlugin implements PluginInterface, EventSubscriberInterface {
30
31
	/**
32
	 * IO object.
33
	 *
34
	 * @var IOInterface IO object.
35
	 */
36
	private $io;
37
38
	/**
39
	 * Composer object.
40
	 *
41
	 * @var Composer Composer object.
42
	 */
43
	private $composer;
44
45
	/**
46
	 * Do nothing.
47
	 *
48
	 * @param Composer    $composer Composer object.
49
	 * @param IOInterface $io IO object.
50
	 */
51
	public function activate( Composer $composer, IOInterface $io ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
52
		$this->composer = $composer;
53
		$this->io       = $io;
54
	}
55
56
	/**
57
	 * Do nothing.
58
	 * phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
59
	 *
60
	 * @param Composer    $composer Composer object.
61
	 * @param IOInterface $io IO object.
62
	 */
63
	public function deactivate( Composer $composer, IOInterface $io ) {
64
		/*
65
		 * Intentionally left empty. This is a PluginInterface method.
66
		 * phpcs:enable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
67
		 */
68
	}
69
70
	/**
71
	 * Do nothing.
72
	 * phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
73
	 *
74
	 * @param Composer    $composer Composer object.
75
	 * @param IOInterface $io IO object.
76
	 */
77
	public function uninstall( Composer $composer, IOInterface $io ) {
78
		/*
79
		 * Intentionally left empty. This is a PluginInterface method.
80
		 * phpcs:enable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
81
		 */
82
	}
83
84
85
	/**
86
	 * Tell composer to listen for events and do something with them.
87
	 *
88
	 * @return array List of subscribed events.
89
	 */
90
	public static function getSubscribedEvents() {
91
		return array(
92
			ScriptEvents::POST_AUTOLOAD_DUMP => 'postAutoloadDump',
93
		);
94
	}
95
96
	/**
97
	 * Generate the custom autolaoder.
98
	 *
99
	 * @param Event $event Script event object.
100
	 */
101
	public function postAutoloadDump( Event $event ) {
102
		$config = $this->composer->getConfig();
103
104
		if ( 'vendor' !== $config->raw()['config']['vendor-dir'] ) {
105
			$this->io->writeError( "\n<error>An error occurred while generating the autoloader files:", true );
106
			$this->io->writeError( 'The project\'s composer.json or composer environment set a non-default vendor directory.', true );
107
			$this->io->writeError( 'The default composer vendor directory must be used.</error>', true );
108
			exit();
109
		}
110
111
		$installationManager = $this->composer->getInstallationManager();
112
		$repoManager         = $this->composer->getRepositoryManager();
113
		$localRepo           = $repoManager->getLocalRepository();
114
		$package             = $this->composer->getPackage();
115
		$optimize            = $event->getFlags()['optimize'];
116
		$suffix              = $this->determineSuffix();
117
118
		$generator = new AutoloadGenerator( $this->io );
119
120
		$generator->dump( $config, $localRepo, $package, $installationManager, 'composer', $optimize, $suffix );
121
		$this->generated = true;
122
	}
123
124
	/**
125
	 * Determine the suffix for the autoloader class.
126
	 *
127
	 * Reuses an existing suffix from vendor/autoload_packages.php or vendor/autoload.php if possible.
128
	 *
129
	 * @return string Suffix.
130
	 */
131
	private function determineSuffix() {
132
		$config     = $this->composer->getConfig();
133
		$vendorPath = $config->get( 'vendor-dir' );
134
135
		// Command line.
136
		$suffix = $config->get( 'autoloader-suffix' );
137
		if ( $suffix ) {
138
			return $suffix;
139
		}
140
141
		// Reuse our own suffix, if any.
142 View Code Duplication
		if ( is_readable( $vendorPath . '/autoload_packages.php' ) ) {
143
			// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
144
			$content = file_get_contents( $vendorPath . '/autoload_packages.php' );
145
			if ( preg_match( '/^namespace Automattic\\\\Jetpack\\\\Autoloader\\\\jp([^;\s]+);/m', $content, $match ) ) {
146
				return $match[1];
147
			}
148
		}
149
150
		// Reuse Composer's suffix, if any.
151 View Code Duplication
		if ( is_readable( $vendorPath . '/autoload.php' ) ) {
152
			// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
153
			$content = file_get_contents( $vendorPath . '/autoload.php' );
154
			if ( preg_match( '{ComposerAutoloaderInit([^:\s]+)::}', $content, $match ) ) {
155
				return $match[1];
156
			}
157
		}
158
159
		// Generate a random suffix.
160
		return md5( uniqid( '', true ) );
161
	}
162
163
}
164