Problem.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\DependencyResolver;
  12. use Composer\Package\CompletePackageInterface;
  13. use Composer\Repository\RepositorySet;
  14. use Composer\Semver\Constraint\Constraint;
  15. /**
  16. * Represents a problem detected while solving dependencies
  17. *
  18. * @author Nils Adermann <naderman@naderman.de>
  19. */
  20. class Problem
  21. {
  22. /**
  23. * A map containing the id of each rule part of this problem as a key
  24. * @var array
  25. */
  26. protected $reasonSeen;
  27. /**
  28. * A set of reasons for the problem, each is a rule or a root require and a rule
  29. * @var array
  30. */
  31. protected $reasons = array();
  32. protected $section = 0;
  33. /**
  34. * Add a rule as a reason
  35. *
  36. * @param Rule $rule A rule which is a reason for this problem
  37. */
  38. public function addRule(Rule $rule)
  39. {
  40. $this->addReason(spl_object_hash($rule), $rule);
  41. }
  42. /**
  43. * Retrieve all reasons for this problem
  44. *
  45. * @return array The problem's reasons
  46. */
  47. public function getReasons()
  48. {
  49. return $this->reasons;
  50. }
  51. /**
  52. * A human readable textual representation of the problem's reasons
  53. *
  54. * @param array $installedMap A map of all present packages
  55. * @return string
  56. */
  57. public function getPrettyString(RepositorySet $repositorySet, Request $request, Pool $pool, array $installedMap = array(), array $learnedPool = array())
  58. {
  59. // TODO doesn't this entirely defeat the purpose of the problem sections? what's the point of sections?
  60. $reasons = call_user_func_array('array_merge', array_reverse($this->reasons));
  61. if (count($reasons) === 1) {
  62. reset($reasons);
  63. $rule = current($reasons);
  64. if (!in_array($rule->getReason(), array(Rule::RULE_ROOT_REQUIRE, Rule::RULE_FIXED), true)) {
  65. throw new \LogicException("Single reason problems must contain a request rule.");
  66. }
  67. $reasonData = $rule->getReasonData();
  68. $packageName = $reasonData['packageName'];
  69. $constraint = $reasonData['constraint'];
  70. if (isset($constraint)) {
  71. $packages = $pool->whatProvides($packageName, $constraint);
  72. } else {
  73. $packages = array();
  74. }
  75. if (empty($packages)) {
  76. return "\n ".implode(self::getMissingPackageReason($repositorySet, $request, $pool, $packageName, $constraint));
  77. }
  78. }
  79. $messages = array();
  80. foreach ($reasons as $rule) {
  81. $messages[] = $rule->getPrettyString($repositorySet, $request, $pool, $installedMap, $learnedPool);
  82. }
  83. return "\n - ".implode("\n - ", $messages);
  84. }
  85. /**
  86. * Store a reason descriptor but ignore duplicates
  87. *
  88. * @param string $id A canonical identifier for the reason
  89. * @param Rule $reason The reason descriptor
  90. */
  91. protected function addReason($id, Rule $reason)
  92. {
  93. // TODO: if a rule is part of a problem description in two sections, isn't this going to remove a message
  94. // that is important to understand the issue?
  95. if (!isset($this->reasonSeen[$id])) {
  96. $this->reasonSeen[$id] = true;
  97. $this->reasons[$this->section][] = $reason;
  98. }
  99. }
  100. public function nextSection()
  101. {
  102. $this->section++;
  103. }
  104. /**
  105. * @internal
  106. */
  107. public static function getMissingPackageReason(RepositorySet $repositorySet, Request $request, Pool $pool, $packageName, $constraint = null)
  108. {
  109. // handle php/hhvm
  110. if ($packageName === 'php' || $packageName === 'php-64bit' || $packageName === 'hhvm') {
  111. $version = phpversion();
  112. $available = $pool->whatProvides($packageName);
  113. if (count($available)) {
  114. $firstAvailable = reset($available);
  115. $version = $firstAvailable->getPrettyVersion();
  116. $extra = $firstAvailable->getExtra();
  117. if ($firstAvailable instanceof CompletePackageInterface && isset($extra['config.platform']) && $extra['config.platform'] === true) {
  118. $version .= '; ' . str_replace('Package ', '', $firstAvailable->getDescription());
  119. }
  120. }
  121. $msg = "- Root composer.json requires ".$packageName.self::constraintToText($constraint).' but ';
  122. if (defined('HHVM_VERSION') || (count($available) && $packageName === 'hhvm')) {
  123. return array($msg, 'your HHVM version does not satisfy that requirement.');
  124. }
  125. if ($packageName === 'hhvm') {
  126. return array($msg, 'you are running this with PHP and not HHVM.');
  127. }
  128. return array($msg, 'your '.$packageName.' version ('. $version .') does not satisfy that requirement.');
  129. }
  130. // handle php extensions
  131. if (0 === stripos($packageName, 'ext-')) {
  132. if (false !== strpos($packageName, ' ')) {
  133. return array('- ', "PHP extension ".$packageName.' should be required as '.str_replace(' ', '-', $packageName).'.');
  134. }
  135. $ext = substr($packageName, 4);
  136. $error = extension_loaded($ext) ? 'it has the wrong version ('.(phpversion($ext) ?: '0').') installed' : 'it is missing from your system';
  137. return array("- Root composer.json requires PHP extension ".$packageName.self::constraintToText($constraint).' but ', $error.'. Install or enable PHP\'s '.$ext.' extension.');
  138. }
  139. // handle linked libs
  140. if (0 === stripos($packageName, 'lib-')) {
  141. if (strtolower($packageName) === 'lib-icu') {
  142. $error = extension_loaded('intl') ? 'it has the wrong version installed, try upgrading the intl extension.' : 'it is missing from your system, make sure the intl extension is loaded.';
  143. return array("- Root composer.json requires linked library ".$packageName.self::constraintToText($constraint).' but ', $error);
  144. }
  145. return array("- Root composer.json requires linked library ".$packageName.self::constraintToText($constraint).' but ', 'it has the wrong version installed or is missing from your system, make sure to load the extension providing it.');
  146. }
  147. $fixedPackage = null;
  148. foreach ($request->getFixedPackages() as $package) {
  149. if ($package->getName() === $packageName) {
  150. $fixedPackage = $package;
  151. if ($pool->isUnacceptableFixedPackage($package)) {
  152. return array("- ", $package->getPrettyName().' is fixed to '.$package->getPrettyVersion().' (lock file version) by a partial update but that version is rejected by your minimum-stability. Make sure you whitelist it for update.');
  153. }
  154. break;
  155. }
  156. }
  157. // first check if the actual requested package is found in normal conditions
  158. // if so it must mean it is rejected by another constraint than the one given here
  159. if ($packages = $repositorySet->findPackages($packageName, $constraint)) {
  160. $rootReqs = $repositorySet->getRootRequires();
  161. if (isset($rootReqs[$packageName])) {
  162. $filtered = array_filter($packages, function ($p) use ($rootReqs, $packageName) {
  163. return $rootReqs[$packageName]->matches(new Constraint('==', $p->getVersion()));
  164. });
  165. if (0 === count($filtered)) {
  166. return array("- Root composer.json requires $packageName".self::constraintToText($constraint) . ', ', 'found '.self::getPackageList($packages).' but '.(self::hasMultipleNames($packages) ? 'these conflict' : 'it conflicts').' with your root composer.json require ('.$rootReqs[$packageName]->getPrettyString().').');
  167. }
  168. }
  169. if ($fixedPackage) {
  170. $fixedConstraint = new Constraint('==', $fixedPackage->getVersion());
  171. $filtered = array_filter($packages, function ($p) use ($fixedConstraint) {
  172. return $fixedConstraint->matches(new Constraint('==', $p->getVersion()));
  173. });
  174. if (0 === count($filtered)) {
  175. return array("- Root composer.json requires $packageName".self::constraintToText($constraint) . ', ', 'found '.self::getPackageList($packages).' but the package is fixed to '.$fixedPackage->getPrettyVersion().' (lock file version) by a partial update and that version does not match. Make sure you whitelist it for update.');
  176. }
  177. }
  178. return array("- Root composer.json requires $packageName".self::constraintToText($constraint) . ', ', 'found '.self::getPackageList($packages).' but '.(self::hasMultipleNames($packages) ? 'these conflict' : 'it conflicts').' with another require.');
  179. }
  180. // check if the package is found when bypassing stability checks
  181. if ($packages = $repositorySet->findPackages($packageName, $constraint, RepositorySet::ALLOW_UNACCEPTABLE_STABILITIES)) {
  182. return array("- Root composer.json requires $packageName".self::constraintToText($constraint) . ', ', 'found '.self::getPackageList($packages).' but '.(self::hasMultipleNames($packages) ? 'these do' : 'it does').' not match your minimum-stability.');
  183. }
  184. // check if the package is found when bypassing the constraint check
  185. if ($packages = $repositorySet->findPackages($packageName, null)) {
  186. // we must first verify if a valid package would be found in a lower priority repository
  187. if ($allReposPackages = $repositorySet->findPackages($packageName, $constraint, RepositorySet::ALLOW_SHADOWED_REPOSITORIES)) {
  188. $higherRepoPackages = $repositorySet->findPackages($packageName, null);
  189. $nextRepoPackages = array();
  190. $nextRepo = null;
  191. foreach ($allReposPackages as $package) {
  192. if ($nextRepo === null || $nextRepo === $package->getRepository()) {
  193. $nextRepoPackages[] = $package;
  194. $nextRepo = $package->getRepository();
  195. } else {
  196. break;
  197. }
  198. }
  199. return array("- Root composer.json requires $packageName".self::constraintToText($constraint) . ', it is ', 'satisfiable by '.self::getPackageList($nextRepoPackages).' from '.$nextRepo->getRepoName().' but '.self::getPackageList($higherRepoPackages).' from '.reset($higherRepoPackages)->getRepository()->getRepoName().' has higher repository priority. The packages with higher priority do not match your constraint and are therefore not installable.');
  200. }
  201. return array("- Root composer.json requires $packageName".self::constraintToText($constraint) . ', ', 'found '.self::getPackageList($packages).' but '.(self::hasMultipleNames($packages) ? 'these do' : 'it does').' not match your constraint.');
  202. }
  203. if (!preg_match('{^[A-Za-z0-9_./-]+$}', $packageName)) {
  204. $illegalChars = preg_replace('{[A-Za-z0-9_./-]+}', '', $packageName);
  205. return array("- Root composer.json requires $packageName, it ", 'could not be found, it looks like its name is invalid, "'.$illegalChars.'" is not allowed in package names.');
  206. }
  207. if ($providers = $repositorySet->getProviders($packageName)) {
  208. $maxProviders = 20;
  209. $providersStr = implode(array_map(function ($p) {
  210. return " - ${p['name']} ".substr($p['description'], 0, 100)."\n";
  211. }, count($providers) > $maxProviders+1 ? array_slice($providers, 0, $maxProviders) : $providers));
  212. if (count($providers) > $maxProviders+1) {
  213. $providersStr .= ' ... and '.(count($providers)-$maxProviders).' more.'."\n";
  214. }
  215. return array("- Root composer.json requires $packageName".self::constraintToText($constraint).", it ", "could not be found in any version, but the following packages provide it: \n".$providersStr." Consider requiring one of these to satisfy the $packageName requirement.");
  216. }
  217. return array("- Root composer.json requires $packageName, it ", "could not be found in any version, there may be a typo in the package name.");
  218. }
  219. /**
  220. * @internal
  221. */
  222. public static function getPackageList(array $packages)
  223. {
  224. $prepared = array();
  225. foreach ($packages as $package) {
  226. $prepared[$package->getName()]['name'] = $package->getPrettyName();
  227. $prepared[$package->getName()]['versions'][$package->getVersion()] = $package->getPrettyVersion();
  228. }
  229. foreach ($prepared as $name => $package) {
  230. $prepared[$name] = $package['name'].'['.implode(', ', $package['versions']).']';
  231. }
  232. return implode(', ', $prepared);
  233. }
  234. private static function hasMultipleNames(array $packages)
  235. {
  236. $name = null;
  237. foreach ($packages as $package) {
  238. if ($name === null || $name === $package->getName()) {
  239. $name = $package->getName();
  240. } else {
  241. return true;
  242. }
  243. }
  244. return false;
  245. }
  246. /**
  247. * Turns a constraint into text usable in a sentence describing a request
  248. *
  249. * @param \Composer\Semver\Constraint\ConstraintInterface $constraint
  250. * @return string
  251. */
  252. protected static function constraintToText($constraint)
  253. {
  254. return $constraint ? ' '.$constraint->getPrettyString() : '';
  255. }
  256. }