Skip to content

Commit dd559b0

Browse files
Split complex methods
1 parent 584a164 commit dd559b0

File tree

2 files changed

+67
-35
lines changed

2 files changed

+67
-35
lines changed

Converter/PackageUtil.php

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,23 +139,17 @@ public static function checkAliasVersion(AssetTypeInterface $assetType, $depende
139139
*/
140140
public static function convertDependencyVersion(AssetTypeInterface $assetType, $dependency, $version)
141141
{
142-
$containsHash = strpos($version, '#') !== false;
142+
$containsHash = false !== strpos($version, '#');
143143
$version = str_replace('#', '', $version);
144-
$version = empty($version) ? '*' : $version;
145-
$version = trim($version);
144+
$version = empty($version) ? '*' : trim($version);
146145
$searchVersion = str_replace(array(' ', '<', '>', '=', '^', '~'), '', $version);
147146

148147
// sha version or branch version
149148
// sha size: 4-40. See https://git-scm.com/book/tr/v2/Git-Tools-Revision-Selection#_short_sha_1
150149
if ($containsHash && preg_match('{^[0-9a-f]{4,40}$}', $version)) {
151150
$version = 'dev-default#'.$version;
152151
} elseif ('*' !== $version && !Validator::validateTag($searchVersion, $assetType) && !static::depIsRange($version)) {
153-
$oldVersion = $version;
154-
$version = 'dev-'.$assetType->getVersionConverter()->convertVersion($version);
155-
156-
if (!Validator::validateBranch($oldVersion)) {
157-
$version .= ' || '.$oldVersion;
158-
}
152+
$version = static::convertBrachVersion($assetType, $version);
159153
}
160154

161155
return array($dependency, $version);
@@ -304,4 +298,24 @@ protected static function depIsRange($version)
304298

305299
return (bool) preg_match('/[\<\>\=\^\~\ ]/', $version);
306300
}
301+
302+
/**
303+
* Convert the dependency branch version.
304+
*
305+
* @param AssetTypeInterface $assetType The asset type
306+
* @param string $version The version
307+
*
308+
* @return string
309+
*/
310+
protected static function convertBrachVersion(AssetTypeInterface $assetType, $version)
311+
{
312+
$oldVersion = $version;
313+
$version = 'dev-'.$assetType->getVersionConverter()->convertVersion($version);
314+
315+
if (!Validator::validateBranch($oldVersion)) {
316+
$version .= ' || '.$oldVersion;
317+
}
318+
319+
return $version;
320+
}
307321
}

Repository/Vcs/GitDriver.php

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Composer\Repository\Vcs\GitDriver as BaseGitDriver;
1717
use Composer\Util\Filesystem;
1818
use Composer\Util\Git as GitUtil;
19+
use Fxp\Composer\AssetPlugin\Repository\AssetRepositoryManager;
1920

2021
/**
2122
* Git vcs driver.
@@ -46,8 +47,8 @@ public function initialize()
4647
{
4748
/* @var AssetRepositoryManager $arm */
4849
$arm = $this->repoConfig['asset-repository-manager'];
49-
5050
$skipSync = false;
51+
5152
if (null !== ($skip = $arm->getConfig()->get('git-skip-update'))) {
5253
$localUrl = $this->config->get('cache-vcs-dir').'/'.preg_replace('{[^a-z0-9.]}i', '-', $this->url).'/';
5354
// check if local copy exists and if it is a git repository and that modification time is within threshold
@@ -57,39 +58,56 @@ public function initialize()
5758
}
5859
}
5960

60-
// copied from parent::initialize()
61-
if (Filesystem::isLocalPath($this->url)) {
62-
$this->url = preg_replace('{[\\/]\.git/?$}', '', $this->url);
63-
$this->repoDir = $this->url;
64-
$cacheUrl = realpath($this->url);
65-
} else {
66-
$this->repoDir = $this->config->get('cache-vcs-dir') . '/' . preg_replace('{[^a-z0-9.]}i', '-', $this->url) . '/';
61+
$cacheUrl = Filesystem::isLocalPath($this->url)
62+
? $this->initializeLocalPath() : $this->initializeRemotePath($skipSync);
63+
$this->getTags();
64+
$this->getBranches();
65+
$this->cache = new Cache($this->io, $this->config->get('cache-repo-dir').'/'.preg_replace('{[^a-z0-9.]}i', '-', $cacheUrl));
66+
}
6767

68-
GitUtil::cleanEnv();
68+
/**
69+
* Initialize the local path.
70+
*
71+
* @return string
72+
*/
73+
private function initializeLocalPath()
74+
{
75+
$this->url = preg_replace('{[\\/]\.git/?$}', '', $this->url);
76+
$this->repoDir = $this->url;
6977

70-
$fs = new Filesystem();
71-
$fs->ensureDirectoryExists(dirname($this->repoDir));
78+
return realpath($this->url);
79+
}
7280

73-
if (!is_writable(dirname($this->repoDir))) {
74-
throw new \RuntimeException('Can not clone '.$this->url.' to access package information. The "'.dirname($this->repoDir).'" directory is not writable by the current user.');
75-
}
81+
/**
82+
* Initialize the remote path.
83+
*
84+
* @param bool $skipSync Check if sync must be skipped
85+
*
86+
* @return string
87+
*/
88+
private function initializeRemotePath($skipSync)
89+
{
90+
$this->repoDir = $this->config->get('cache-vcs-dir').'/'.preg_replace('{[^a-z0-9.]}i', '-', $this->url).'/';
7691

77-
if (preg_match('{^ssh://[^@]+@[^:]+:[^0-9]+}', $this->url)) {
78-
throw new \InvalidArgumentException('The source URL '.$this->url.' is invalid, ssh URLs should have a port number after ":".'."\n".'Use ssh://git@example.com:22/path or just git@example.com:path if you do not want to provide a password or custom port.');
79-
}
92+
GitUtil::cleanEnv();
8093

81-
$gitUtil = new GitUtil($this->io, $this->config, $this->process, $fs);
82-
// patched line, sync from local dir without modifying url
83-
if (!$skipSync && !$gitUtil->syncMirror($this->url, $this->repoDir)) {
84-
$this->io->writeError('<error>Failed to update '.$this->url.', package information from this repository may be outdated</error>');
85-
}
94+
$fs = new Filesystem();
95+
$fs->ensureDirectoryExists(dirname($this->repoDir));
8696

87-
$cacheUrl = $this->url;
97+
if (!is_writable(dirname($this->repoDir))) {
98+
throw new \RuntimeException('Can not clone '.$this->url.' to access package information. The "'.dirname($this->repoDir).'" directory is not writable by the current user.');
8899
}
89100

90-
$this->getTags();
91-
$this->getBranches();
101+
if (preg_match('{^ssh://[^@]+@[^:]+:[^0-9]+}', $this->url)) {
102+
throw new \InvalidArgumentException('The source URL '.$this->url.' is invalid, ssh URLs should have a port number after ":".'."\n".'Use ssh://git@example.com:22/path or just git@example.com:path if you do not want to provide a password or custom port.');
103+
}
92104

93-
$this->cache = new Cache($this->io, $this->config->get('cache-repo-dir').'/'.preg_replace('{[^a-z0-9.]}i', '-', $cacheUrl));
105+
$gitUtil = new GitUtil($this->io, $this->config, $this->process, $fs);
106+
// patched line, sync from local dir without modifying url
107+
if (!$skipSync && !$gitUtil->syncMirror($this->url, $this->repoDir)) {
108+
$this->io->writeError('<error>Failed to update '.$this->url.', package information from this repository may be outdated</error>');
109+
}
110+
111+
return $this->url;
94112
}
95113
}

0 commit comments

Comments
 (0)