Skip to content

Commit

Permalink
Merge pull request #9187 from simonberger/substring-to-strpos
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Sep 8, 2020
2 parents 8694077 + 7859fe7 commit 7604c36
Show file tree
Hide file tree
Showing 23 changed files with 60 additions and 66 deletions.
2 changes: 1 addition & 1 deletion src/Composer/Autoload/ClassMapGenerator.php
Expand Up @@ -251,7 +251,7 @@ private static function findClasses($path)
// strip strings
$contents = preg_replace('{"[^"\\\\]*+(\\\\.[^"\\\\]*+)*+"|\'[^\'\\\\]*+(\\\\.[^\'\\\\]*+)*+\'}s', 'null', $contents);
// strip leading non-php code if needed
if (substr($contents, 0, 2) !== '<?') {
if (strpos($contents, '<?') !== 0) {
$contents = preg_replace('{^.+?<\?}s', '<?', $contents, 1, $replacements);
if ($replacements === 0) {
return array();
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Command/ConfigCommand.php
Expand Up @@ -565,7 +565,7 @@ function ($vals) {
),
);

if ($input->getOption('global') && (isset($uniqueProps[$settingKey]) || isset($multiProps[$settingKey]) || substr($settingKey, 0, 6) === 'extra.')) {
if ($input->getOption('global') && (isset($uniqueProps[$settingKey]) || isset($multiProps[$settingKey]) || strpos($settingKey, 'extra.') === 0)) {
throw new \InvalidArgumentException('The '.$settingKey.' property can not be set in the global config.json file. Use `composer global config` to apply changes to the global composer.json');
}
if ($input->getOption('unset') && (isset($uniqueProps[$settingKey]) || isset($multiProps[$settingKey]))) {
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Command/DiagnoseCommand.php
Expand Up @@ -148,7 +148,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$io->write('Checking disk free space: ', false);
$this->outputResult($this->checkDiskSpace($config));

if ('phar:' === substr(__FILE__, 0, 5)) {
if (strpos(__FILE__, 'phar:') === 0) {
$io->write('Checking pubkeys: ', false);
$this->outputResult($this->checkPubKeys($config));

Expand Down
4 changes: 2 additions & 2 deletions src/Composer/Command/InitCommand.php
Expand Up @@ -120,7 +120,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

$file = new JsonFile(Factory::getComposerFile());
$json = $file->encode($options);
$json = JsonFile::encode($options);

if ($input->isInteractive()) {
$io->writeError(array('', $json, ''));
Expand Down Expand Up @@ -655,7 +655,7 @@ protected function addVendorIgnore($ignoreFile, $vendor = '/vendor/')
if (file_exists($ignoreFile)) {
$contents = file_get_contents($ignoreFile);

if ("\n" !== substr($contents, 0, -1)) {
if (strpos($contents, "\n") !== 0) {
$contents .= "\n";
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Command/SelfUpdateCommand.php
Expand Up @@ -166,7 +166,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
}

if ($requestedChannel && is_numeric($requestedChannel) && substr($latestStable['version'], 0, 1) !== $requestedChannel) {
if ($requestedChannel && is_numeric($requestedChannel) && strpos($latestStable['version'], $requestedChannel) !== 0) {
$io->writeError('<warning>Warning: You forced the install of '.$latestVersion.' via --'.$requestedChannel.', but '.$latestStable['version'].' is the latest stable version. Updating to it via composer self-update --stable is recommended.</warning>');
}

Expand Down
5 changes: 2 additions & 3 deletions src/Composer/Config/JsonConfigSource.php
Expand Up @@ -135,7 +135,7 @@ public function removeConfigSetting($name)
public function addProperty($name, $value)
{
$this->manipulateJson('addProperty', $name, $value, function (&$config, $key, $val) {
if (substr($key, 0, 6) === 'extra.' || substr($key, 0, 8) === 'scripts.') {
if (strpos($key, 'extra.') === 0 || strpos($key, 'scripts.') === 0) {
$bits = explode('.', $key);
$last = array_pop($bits);
$arr = &$config[reset($bits)];
Expand All @@ -157,9 +157,8 @@ public function addProperty($name, $value)
*/
public function removeProperty($name)
{
$authConfig = $this->authConfig;
$this->manipulateJson('removeProperty', $name, function (&$config, $key) {
if (substr($key, 0, 6) === 'extra.' || substr($key, 0, 8) === 'scripts.') {
if (strpos($key, 'extra.') === 0 || strpos($key, 'scripts.') === 0) {
$bits = explode('.', $key);
$last = array_pop($bits);
$arr = &$config[reset($bits)];
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Console/Application.php
Expand Up @@ -473,7 +473,7 @@ protected function getDefaultCommands()
new Command\FundCommand(),
));

if ('phar:' === substr(__FILE__, 0, 5)) {
if (strpos(__FILE__, 'phar:') === 0) {
$commands[] = new Command\SelfUpdateCommand();
}

Expand Down
4 changes: 2 additions & 2 deletions src/Composer/DependencyResolver/DefaultPolicy.php
Expand Up @@ -116,8 +116,8 @@ public function compareByPriority(Pool $pool, PackageInterface $a, PackageInterf
if ($requiredPackage && false !== ($pos = strpos($requiredPackage, '/'))) {
$requiredVendor = substr($requiredPackage, 0, $pos);

$aIsSameVendor = substr($a->getName(), 0, $pos) === $requiredVendor;
$bIsSameVendor = substr($b->getName(), 0, $pos) === $requiredVendor;
$aIsSameVendor = strpos($a->getName(), $requiredVendor) === 0;
$bIsSameVendor = strpos($b->getName(), $requiredVendor) === 0;

if ($bIsSameVendor !== $aIsSameVendor) {
return $aIsSameVendor ? -1 : 1;
Expand Down
16 changes: 6 additions & 10 deletions src/Composer/EventDispatcher/EventDispatcher.php
Expand Up @@ -12,17 +12,12 @@

namespace Composer\EventDispatcher;

use Composer\DependencyResolver\PolicyInterface;
use Composer\DependencyResolver\Request;
use Composer\DependencyResolver\Pool;
use Composer\DependencyResolver\Transaction;
use Composer\Installer\InstallerEvent;
use Composer\IO\IOInterface;
use Composer\Composer;
use Composer\DependencyResolver\Operation\OperationInterface;
use Composer\Repository\CompositeRepository;
use Composer\Repository\RepositoryInterface;
use Composer\Repository\RepositorySet;
use Composer\Script;
use Composer\Installer\PackageEvent;
use Composer\Installer\BinaryInstaller;
Expand Down Expand Up @@ -177,7 +172,7 @@ protected function doDispatch(Event $event)

$args = array_merge($script, $event->getArguments());
$flags = $event->getFlags();
if (substr($callable, 0, 10) === '@composer ') {
if (strpos($callable, '@composer ') === 0) {
$exec = $this->getPhpExecCommand() . ' ' . ProcessExecutor::escape(getenv('COMPOSER_BINARY')) . ' ' . implode(' ', $args);
if (0 !== ($exitCode = $this->executeTty($exec))) {
$this->io->writeError(sprintf('<error>Script %s handling the %s event returned with error code '.$exitCode.'</error>', $callable, $event->getName()), true, IOInterface::QUIET);
Expand Down Expand Up @@ -239,11 +234,12 @@ protected function doDispatch(Event $event)
}
}

if (substr($exec, 0, 8) === '@putenv ') {
if (strpos($exec, '@putenv ') === 0) {
putenv(substr($exec, 8));

continue;
} elseif (substr($exec, 0, 5) === '@php ') {
}
if (strpos($exec, '@php ') === 0) {
$exec = $this->getPhpExecCommand() . ' ' . substr($exec, 5);
} else {
$finder = new PhpExecutableFinder();
Expand All @@ -257,7 +253,7 @@ protected function doDispatch(Event $event)
// if composer is being executed, make sure it runs the expected composer from current path
// resolution, even if bin-dir contains composer too because the project requires composer/composer
// see https://github.com/composer/composer/issues/8748
if (substr($exec, 0, 9) === 'composer ') {
if (strpos($exec, 'composer ') === 0) {
$exec = $this->getPhpExecCommand() . ' ' . ProcessExecutor::escape(getenv('COMPOSER_BINARY')) . substr($exec, 8);
}

Expand Down Expand Up @@ -472,7 +468,7 @@ protected function isPhpScript($callable)
*/
protected function isComposerScript($callable)
{
return '@' === substr($callable, 0, 1) && '@php ' !== substr($callable, 0, 5) && '@putenv ' !== substr($callable, 0, 8);
return strpos($callable, '@') === 0 && strpos($callable, '@php ') !== 0 && strpos($callable, '@putenv ') !== 0;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Factory.php
Expand Up @@ -651,7 +651,7 @@ public static function createHttpDownloader(IOInterface $io, Config $config, $op
private static function useXdg()
{
foreach (array_keys($_SERVER) as $key) {
if (substr($key, 0, 4) === 'XDG_') {
if (strpos($key, 'XDG_') === 0) {
return true;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Composer/Installer/LibraryInstaller.php
Expand Up @@ -262,8 +262,8 @@ protected function updateCode(PackageInterface $initial, PackageInterface $targe
if ($targetDownloadPath !== $initialDownloadPath) {
// if the target and initial dirs intersect, we force a remove + install
// to avoid the rename wiping the target dir as part of the initial dir cleanup
if (substr($initialDownloadPath, 0, strlen($targetDownloadPath)) === $targetDownloadPath
|| substr($targetDownloadPath, 0, strlen($initialDownloadPath)) === $initialDownloadPath
if (strpos($initialDownloadPath, $targetDownloadPath) === 0
|| strpos($targetDownloadPath, $initialDownloadPath) === 0
) {
$promise = $this->removeCode($initial);
if (!$promise instanceof PromiseInterface) {
Expand Down
12 changes: 6 additions & 6 deletions src/Composer/Json/JsonManipulator.php
Expand Up @@ -167,15 +167,15 @@ public function removeConfigSetting($name)

public function addProperty($name, $value)
{
if (substr($name, 0, 8) === 'suggest.') {
if (strpos($name, 'suggest.') === 0) {
return $this->addSubNode('suggest', substr($name, 8), $value);
}

if (substr($name, 0, 6) === 'extra.') {
if (strpos($name, 'extra.') === 0) {
return $this->addSubNode('extra', substr($name, 6), $value);
}

if (substr($name, 0, 8) === 'scripts.') {
if (strpos($name, 'scripts.') === 0) {
return $this->addSubNode('scripts', substr($name, 8), $value);
}

Expand All @@ -184,15 +184,15 @@ public function addProperty($name, $value)

public function removeProperty($name)
{
if (substr($name, 0, 8) === 'suggest.') {
if (strpos($name, 'suggest.') === 0) {
return $this->removeSubNode('suggest', substr($name, 8));
}

if (substr($name, 0, 6) === 'extra.') {
if (strpos($name, 'extra.') === 0) {
return $this->removeSubNode('extra', substr($name, 6));
}

if (substr($name, 0, 8) === 'scripts.') {
if (strpos($name, 'scripts.') === 0) {
return $this->removeSubNode('scripts', substr($name, 8));
}

Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Package/Loader/ArrayLoader.php
Expand Up @@ -334,7 +334,7 @@ private function createLink($source, $sourceVersion, $description, $target, $pre
*/
public function getBranchAlias(array $config)
{
if ('dev-' !== substr($config['version'], 0, 4) && '-dev' !== substr($config['version'], -4)) {
if (strpos($config['version'], 'dev-') !== 0 && '-dev' !== substr($config['version'], -4)) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Package/Loader/ValidatingArrayLoader.php
Expand Up @@ -256,7 +256,7 @@ public function load(array $config, $class = 'Composer\Package\CompletePackage')
// check requires for exact constraints
($this->flags & self::CHECK_STRICT_CONSTRAINTS)
&& 'require' === $linkType
&& substr($linkConstraint, 0, 1) === '='
&& strpos($linkConstraint, '=') === 0
&& $stableConstraint->versionCompare($stableConstraint, $linkConstraint, '<=')
) {
$this->warnings[] = $linkType.'.'.$package.' : exact version constraints ('.$constraint.') should be avoided if the package follows semantic versioning';
Expand Down
10 changes: 6 additions & 4 deletions src/Composer/Package/Version/VersionGuesser.php
Expand Up @@ -20,8 +20,6 @@
use Composer\Util\HttpDownloader;
use Composer\Util\ProcessExecutor;
use Composer\Util\Svn as SvnUtil;
use Composer\Util\Platform;
use Composer\Package\Version\VersionParser;


/**
Expand Down Expand Up @@ -96,7 +94,7 @@ public function guessVersion(array $packageConfig, $path)

private function postprocess(array $versionData)
{
if (!empty($versionData['feature_version']) && $versionData['feature_version'] === $versionData['version'] && $versionData['feature_pretty_version'] === $versionData['feature_pretty_version']) {
if (!empty($versionData['feature_version']) && $versionData['feature_version'] === $versionData['version'] && $versionData['feature_pretty_version'] === $versionData['pretty_version']) {
unset($versionData['feature_version'], $versionData['feature_pretty_version']);
}

Expand Down Expand Up @@ -129,7 +127,11 @@ private function guessGitVersion(array $packageConfig, $path)
// find current branch and collect all branch names
foreach ($this->process->splitLines($output) as $branch) {
if ($branch && preg_match('{^(?:\* ) *(\(no branch\)|\(detached from \S+\)|\(HEAD detached at \S+\)|\S+) *([a-f0-9]+) .*$}', $branch, $match)) {
if ($match[1] === '(no branch)' || substr($match[1], 0, 10) === '(detached ' || substr($match[1], 0, 17) === '(HEAD detached at') {
if (
$match[1] === '(no branch)'
|| strpos($match[1], '(detached ') === 0
|| strpos($match[1], '(HEAD detached at') === 0
) {
$version = 'dev-' . $match[2];
$prettyVersion = $version;
$isFeatureBranch = true;
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Package/Version/VersionParser.php
Expand Up @@ -79,7 +79,7 @@ public static function isUpgrade($normalizedFrom, $normalizedTo)
$normalizedFrom = str_replace(array('dev-master', 'dev-trunk', 'dev-default'), VersionParser::DEFAULT_BRANCH_ALIAS, $normalizedFrom);
$normalizedTo = str_replace(array('dev-master', 'dev-trunk', 'dev-default'), VersionParser::DEFAULT_BRANCH_ALIAS, $normalizedTo);

if (substr($normalizedFrom, 0, 4) === 'dev-' || substr($normalizedTo, 0, 4) === 'dev-') {
if (strpos($normalizedFrom, 'dev-') === 0 || strpos($normalizedTo, 'dev-') === 0) {
return true;
}

Expand Down
5 changes: 1 addition & 4 deletions src/Composer/Repository/ComposerRepository.php
Expand Up @@ -20,8 +20,6 @@
use Composer\Json\JsonFile;
use Composer\Cache;
use Composer\Config;
use Composer\Composer;
use Composer\Factory;
use Composer\IO\IOInterface;
use Composer\Semver\CompilingMatcher;
use Composer\Util\HttpDownloader;
Expand All @@ -36,7 +34,6 @@
use Composer\Util\Http\Response;
use Composer\Util\MetadataMinifier;
use Composer\Util\Url;
use React\Promise\Promise;

/**
* @author Jordi Boggiano <j.boggiano@seld.be>
Expand Down Expand Up @@ -815,7 +812,7 @@ protected function loadRootServerFile()
return $this->rootData;
}

if (!extension_loaded('openssl') && 'https' === substr($this->url, 0, 5)) {
if (!extension_loaded('openssl') && strpos($this->url, 'https') === 0) {
throw new \RuntimeException('You must enable the openssl extension in your php.ini to load information from '.$this->url);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Repository/RepositoryFactory.php
Expand Up @@ -46,7 +46,7 @@ public static function configFromString(IOInterface $io, Config $config, $reposi
} else {
throw new \InvalidArgumentException("Invalid repository URL ($repository) given. This file does not contain a valid composer repository.");
}
} elseif ('{' === substr($repository, 0, 1)) {
} elseif (strpos($repository, '{') === 0) {
// assume it is a json object that makes a repo config
$repoConfig = JsonFile::parseJson($repository);
} else {
Expand Down
7 changes: 4 additions & 3 deletions src/Composer/Repository/VcsRepository.php
Expand Up @@ -302,10 +302,10 @@ protected function initialize()
}

// make sure branch packages have a dev flag
if ('dev-' === substr($parsedBranch, 0, 4) || VersionParser::DEFAULT_BRANCH_ALIAS === $parsedBranch) {
if (strpos($parsedBranch, 'dev-') === 0 || VersionParser::DEFAULT_BRANCH_ALIAS === $parsedBranch) {
$version = 'dev-' . $branch;
} else {
$prefix = substr($branch, 0, 1) === 'v' ? 'v' : '';
$prefix = strpos($branch, 'v') === 0 ? 'v' : '';
$version = $prefix . preg_replace('{(\.9{7})+}', '.x', $parsedBranch);
}

Expand All @@ -314,7 +314,8 @@ protected function initialize()
$this->addPackage($cachedPackage);

continue;
} elseif ($cachedPackage === false) {
}
if ($cachedPackage === false) {
$this->emptyReferences[] = $identifier;

continue;
Expand Down
4 changes: 2 additions & 2 deletions src/Composer/Util/Filesystem.php
Expand Up @@ -444,7 +444,7 @@ public function findShortestPathCode($from, $to, $directories = false, $staticCo
*/
public function isAbsolutePath($path)
{
return substr($path, 0, 1) === '/' || substr($path, 1, 1) === ':' || substr($path, 0, 2) === '\\\\';
return strpos($path, '/') === 0 || substr($path, 1, 1) === ':' || strpos($path, '\\\\') === 0;
}

/**
Expand Down Expand Up @@ -487,7 +487,7 @@ public function normalizePath($path)
$path = substr($path, \strlen($prefix));
}

if (substr($path, 0, 1) === '/') {
if (strpos($path, '/') === 0) {
$absolute = true;
$path = substr($path, 1);
}
Expand Down

0 comments on commit 7604c36

Please sign in to comment.