Completed
Push — update/travis-74 ( 386fcf...69a56a )
by
unknown
224:04 queued 215:38
created

CustomAutoloaderPlugin   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 133
Duplicated Lines 10.53 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 14
loc 133
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 21 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\EventDispatcher\EventSubscriberInterface;
19
use Composer\IO\IOInterface;
20
use Composer\Plugin\PluginInterface;
21
use Composer\Script\Event;
22
use Composer\Script\ScriptEvents;
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
	 * Tell composer to listen for events and do something with them.
86
	 *
87
	 * @return array List of subscribed events.
88
	 */
89
	public static function getSubscribedEvents() {
90
		return array(
91
			ScriptEvents::POST_AUTOLOAD_DUMP => 'postAutoloadDump',
92
		);
93
	}
94
95
	/**
96
	 * Generate the custom autolaoder.
97
	 *
98
	 * @param Event $event Script event object.
99
	 */
100
	public function postAutoloadDump( Event $event ) {
101
		$config = $this->composer->getConfig();
102
103
		if ( 'vendor' !== $config->raw()['config']['vendor-dir'] ) {
104
			$this->io->writeError( "\n<error>An error occurred while generating the autoloader files:", true );
105
			$this->io->writeError( 'The project\'s composer.json or composer environment set a non-default vendor directory.', true );
106
			$this->io->writeError( 'The default composer vendor directory must be used.</error>', true );
107
			exit();
108
		}
109
110
		$installationManager = $this->composer->getInstallationManager();
111
		$repoManager         = $this->composer->getRepositoryManager();
112
		$localRepo           = $repoManager->getLocalRepository();
113
		$package             = $this->composer->getPackage();
114
		$optimize            = $event->getFlags()['optimize'];
115
		$suffix              = $this->determineSuffix();
116
117
		$generator = new AutoloadGenerator( $this->io );
118
		$generator->dump( $this->composer, $config, $localRepo, $package, $installationManager, 'composer', $optimize, $suffix );
119
		$this->generated = true;
120
	}
121
122
	/**
123
	 * Determine the suffix for the autoloader class.
124
	 *
125
	 * Reuses an existing suffix from vendor/autoload_packages.php or vendor/autoload.php if possible.
126
	 *
127
	 * @return string Suffix.
128
	 */
129
	private function determineSuffix() {
130
		$config     = $this->composer->getConfig();
131
		$vendorPath = $config->get( 'vendor-dir' );
132
133
		// Command line.
134
		$suffix = $config->get( 'autoloader-suffix' );
135
		if ( $suffix ) {
136
			return $suffix;
137
		}
138
139
		// Reuse our own suffix, if any.
140 View Code Duplication
		if ( is_readable( $vendorPath . '/autoload_packages.php' ) ) {
141
			// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
142
			$content = file_get_contents( $vendorPath . '/autoload_packages.php' );
143
			if ( preg_match( '/^namespace Automattic\\\\Jetpack\\\\Autoloader\\\\jp([^;\s]+);/m', $content, $match ) ) {
144
				return $match[1];
145
			}
146
		}
147
148
		// Reuse Composer's suffix, if any.
149 View Code Duplication
		if ( is_readable( $vendorPath . '/autoload.php' ) ) {
150
			// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
151
			$content = file_get_contents( $vendorPath . '/autoload.php' );
152
			if ( preg_match( '{ComposerAutoloaderInit([^:\s]+)::}', $content, $match ) ) {
153
				return $match[1];
154
			}
155
		}
156
157
		// Generate a random suffix.
158
		return md5( uniqid( '', true ) );
159
	}
160
161
}
162