Conditions | 1 |
Paths | 1 |
Total Lines | 114 |
Code Lines | 50 |
Lines | 0 |
Ratio | 0 % |
Changes | 6 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
85 | public function testDumpVersionsClass() |
||
86 | { |
||
87 | $config = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock(); |
||
88 | $locker = $this->getMockBuilder(Locker::class)->disableOriginalConstructor()->getMock(); |
||
89 | $autoloadGenerator = $this->getMockBuilder(AutoloadGenerator::class)->disableOriginalConstructor()->getMock(); |
||
90 | $repositoryManager = $this->getMockBuilder(RepositoryManager::class)->disableOriginalConstructor()->getMock(); |
||
91 | $installManager = $this->getMockBuilder(InstallationManager::class)->disableOriginalConstructor()->getMock(); |
||
92 | $repository = $this->getMock(InstalledRepositoryInterface::class); |
||
93 | $package = $this->getMock(RootPackageInterface::class); |
||
94 | |||
95 | $vendorDir = sys_get_temp_dir() . '/' . uniqid('InstallerTest', true); |
||
96 | |||
97 | $expectedPath = $vendorDir . '/ocramius/package-versions/src/PackageVersions'; |
||
98 | |||
99 | mkdir($expectedPath, 0777, true); |
||
100 | |||
101 | $locker |
||
102 | ->expects(self::any()) |
||
103 | ->method('getLockData') |
||
104 | ->willReturn([ |
||
105 | 'packages' => [ |
||
106 | [ |
||
107 | 'name' => 'foo/bar', |
||
108 | 'version' => '1.2.3', |
||
109 | 'source' => [ |
||
110 | 'reference' => 'abc123', |
||
111 | ], |
||
112 | ], |
||
113 | [ |
||
114 | 'name' => 'baz/tab', |
||
115 | 'version' => '4.5.6', |
||
116 | 'source' => [ |
||
117 | 'reference' => 'def456', |
||
118 | ], |
||
119 | ], |
||
120 | ], |
||
121 | 'packages-dev' => [ |
||
122 | [ |
||
123 | 'name' => 'tar/taz', |
||
124 | 'version' => '7.8.9', |
||
125 | 'source' => [ |
||
126 | 'reference' => 'ghi789', |
||
127 | ], |
||
128 | ] |
||
129 | ], |
||
130 | ]); |
||
131 | |||
132 | $autoloadGenerator->expects(self::once())->method('dump'); |
||
133 | $repositoryManager->expects(self::any())->method('getLocalRepository')->willReturn($repository); |
||
134 | |||
135 | $this->composer->expects(self::any())->method('getConfig')->willReturn($config); |
||
136 | $this->composer->expects(self::any())->method('getLocker')->willReturn($locker); |
||
137 | $this->composer->expects(self::any())->method('getAutoloadGenerator')->willReturn($autoloadGenerator); |
||
138 | $this->composer->expects(self::any())->method('getRepositoryManager')->willReturn($repositoryManager); |
||
139 | $this->composer->expects(self::any())->method('getPackage')->willReturn($package); |
||
140 | $this->composer->expects(self::any())->method('getInstallationManager')->willReturn($installManager); |
||
141 | |||
142 | $package->expects(self::any())->method('getName')->willReturn('root/package'); |
||
143 | $package->expects(self::any())->method('getVersion')->willReturn('1.3.5'); |
||
144 | $package->expects(self::any())->method('getSourceReference')->willReturn('aaabbbcccddd'); |
||
145 | |||
146 | $config->expects(self::any())->method('get')->with('vendor-dir')->willReturn($vendorDir); |
||
147 | |||
148 | Installer::dumpVersionsClass(new Event( |
||
149 | 'post-install-cmd', |
||
150 | $this->composer, |
||
151 | $this->io |
||
152 | )); |
||
153 | |||
154 | $expectedSource = <<<'PHP' |
||
155 | <?php |
||
156 | |||
157 | namespace PackageVersions; |
||
158 | |||
159 | /** |
||
160 | * This class is generated by ocramius/package-versions, specifically by |
||
161 | * @see \PackageVersions\Installer |
||
162 | * |
||
163 | * This file is overwritten at every run of `composer install` or `composer update`. |
||
164 | */ |
||
165 | final class Versions |
||
166 | { |
||
167 | const VERSIONS = array ( |
||
168 | 'foo/bar' => '1.2.3@abc123', |
||
169 | 'baz/tab' => '4.5.6@def456', |
||
170 | 'tar/taz' => '7.8.9@ghi789', |
||
171 | 'root/package' => '1.3.5@aaabbbcccddd', |
||
172 | ); |
||
173 | |||
174 | private function __construct() |
||
175 | { |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * @throws \OutOfBoundsException if a version cannot be located |
||
180 | */ |
||
181 | public static function getVersion(string $packageName) : string |
||
182 | { |
||
183 | if (! isset(self::VERSIONS[$packageName])) { |
||
184 | throw new \OutOfBoundsException( |
||
185 | 'Required package "' . $packageName . '" is not installed: cannot detect its version' |
||
186 | ); |
||
187 | } |
||
188 | |||
189 | return self::VERSIONS[$packageName]; |
||
190 | } |
||
191 | } |
||
192 | |||
193 | PHP; |
||
194 | |||
195 | self::assertSame($expectedSource, file_get_contents($expectedPath . '/Versions.php')); |
||
196 | |||
197 | $this->rmDir($vendorDir); |
||
198 | } |
||
199 | |||
330 |