ConfigValidator.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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\Util;
  12. use Composer\Package\Loader\ArrayLoader;
  13. use Composer\Package\Loader\ValidatingArrayLoader;
  14. use Composer\Package\Loader\InvalidPackageException;
  15. use Composer\Json\JsonValidationException;
  16. use Composer\IO\IOInterface;
  17. use Composer\Json\JsonFile;
  18. use Composer\Spdx\SpdxLicenses;
  19. /**
  20. * Validates a composer configuration.
  21. *
  22. * @author Robert Schönthal <seroscho@googlemail.com>
  23. * @author Jordi Boggiano <j.boggiano@seld.be>
  24. */
  25. class ConfigValidator
  26. {
  27. private $io;
  28. public function __construct(IOInterface $io)
  29. {
  30. $this->io = $io;
  31. }
  32. /**
  33. * Validates the config, and returns the result.
  34. *
  35. * @param string $file The path to the file
  36. * @param int $arrayLoaderValidationFlags Flags for ArrayLoader validation
  37. *
  38. * @return array a triple containing the errors, publishable errors, and warnings
  39. */
  40. public function validate($file, $arrayLoaderValidationFlags = ValidatingArrayLoader::CHECK_ALL)
  41. {
  42. $errors = array();
  43. $publishErrors = array();
  44. $warnings = array();
  45. // validate json schema
  46. $laxValid = false;
  47. try {
  48. $json = new JsonFile($file, null, $this->io);
  49. $manifest = $json->read();
  50. $json->validateSchema(JsonFile::LAX_SCHEMA);
  51. $laxValid = true;
  52. $json->validateSchema();
  53. } catch (JsonValidationException $e) {
  54. foreach ($e->getErrors() as $message) {
  55. if ($laxValid) {
  56. $publishErrors[] = $message;
  57. } else {
  58. $errors[] = $message;
  59. }
  60. }
  61. } catch (\Exception $e) {
  62. $errors[] = $e->getMessage();
  63. return array($errors, $publishErrors, $warnings);
  64. }
  65. // validate actual data
  66. if (empty($manifest['license'])) {
  67. $warnings[] = 'No license specified, it is recommended to do so. For closed-source software you may use "proprietary" as license.';
  68. } else {
  69. $licenses = (array) $manifest['license'];
  70. // strip proprietary since it's not a valid SPDX identifier, but is accepted by composer
  71. foreach ($licenses as $key => $license) {
  72. if ('proprietary' === $license) {
  73. unset($licenses[$key]);
  74. }
  75. }
  76. $licenseValidator = new SpdxLicenses();
  77. foreach ($licenses as $license) {
  78. $spdxLicense = $licenseValidator->getLicenseByIdentifier($license);
  79. if ($spdxLicense && $spdxLicense[3]) {
  80. if (preg_match('{^[AL]?GPL-[123](\.[01])?\+$}i', $license)) {
  81. $warnings[] = sprintf(
  82. 'License "%s" is a deprecated SPDX license identifier, use "'.str_replace('+', '', $license).'-or-later" instead',
  83. $license
  84. );
  85. } elseif (preg_match('{^[AL]?GPL-[123](\.[01])?$}i', $license)) {
  86. $warnings[] = sprintf(
  87. 'License "%s" is a deprecated SPDX license identifier, use "'.$license.'-only" or "'.$license.'-or-later" instead',
  88. $license
  89. );
  90. } else {
  91. $warnings[] = sprintf(
  92. 'License "%s" is a deprecated SPDX license identifier, see https://spdx.org/licenses/',
  93. $license
  94. );
  95. }
  96. }
  97. }
  98. }
  99. if (isset($manifest['version'])) {
  100. $warnings[] = 'The version field is present, it is recommended to leave it out if the package is published on Packagist.';
  101. }
  102. if (!empty($manifest['name']) && preg_match('{[A-Z]}', $manifest['name'])) {
  103. $suggestName = preg_replace('{(?:([a-z])([A-Z])|([A-Z])([A-Z][a-z]))}', '\\1\\3-\\2\\4', $manifest['name']);
  104. $suggestName = strtolower($suggestName);
  105. $publishErrors[] = sprintf(
  106. 'Name "%s" does not match the best practice (e.g. lower-cased/with-dashes). We suggest using "%s" instead. As such you will not be able to submit it to Packagist.',
  107. $manifest['name'],
  108. $suggestName
  109. );
  110. }
  111. if (!empty($manifest['type']) && $manifest['type'] == 'composer-installer') {
  112. $warnings[] = "The package type 'composer-installer' is deprecated. Please distribute your custom installers as plugins from now on. See https://getcomposer.org/doc/articles/plugins.md for plugin documentation.";
  113. }
  114. // check for require-dev overrides
  115. if (isset($manifest['require']) && isset($manifest['require-dev'])) {
  116. $requireOverrides = array_intersect_key($manifest['require'], $manifest['require-dev']);
  117. if (!empty($requireOverrides)) {
  118. $plural = (count($requireOverrides) > 1) ? 'are' : 'is';
  119. $warnings[] = implode(', ', array_keys($requireOverrides)). " {$plural} required both in require and require-dev, this can lead to unexpected behavior";
  120. }
  121. }
  122. // check for commit references
  123. $require = isset($manifest['require']) ? $manifest['require'] : array();
  124. $requireDev = isset($manifest['require-dev']) ? $manifest['require-dev'] : array();
  125. $packages = array_merge($require, $requireDev);
  126. foreach ($packages as $package => $version) {
  127. if (preg_match('/#/', $version) === 1) {
  128. $warnings[] = sprintf(
  129. 'The package "%s" is pointing to a commit-ref, this is bad practice and can cause unforeseen issues.',
  130. $package
  131. );
  132. }
  133. }
  134. // report scripts-descriptions for non-existent scripts
  135. $scriptsDescriptions = isset($manifest['scripts-descriptions']) ? $manifest['scripts-descriptions'] : array();
  136. $scripts = isset($manifest['scripts']) ? $manifest['scripts'] : array();
  137. foreach ($scriptsDescriptions as $scriptName => $scriptDescription) {
  138. if (!array_key_exists($scriptName, $scripts)) {
  139. $warnings[] = sprintf(
  140. 'Description for non-existent script "%s" found in "scripts-descriptions"',
  141. $scriptName
  142. );
  143. }
  144. }
  145. // check for empty psr-0/psr-4 namespace prefixes
  146. if (isset($manifest['autoload']['psr-0'][''])) {
  147. $warnings[] = "Defining autoload.psr-0 with an empty namespace prefix is a bad idea for performance";
  148. }
  149. if (isset($manifest['autoload']['psr-4'][''])) {
  150. $warnings[] = "Defining autoload.psr-4 with an empty namespace prefix is a bad idea for performance";
  151. }
  152. $loader = new ValidatingArrayLoader(new ArrayLoader(), true, null, $arrayLoaderValidationFlags);
  153. try {
  154. if (!isset($manifest['version'])) {
  155. $manifest['version'] = '1.0.0';
  156. }
  157. if (!isset($manifest['name'])) {
  158. $manifest['name'] = 'dummy/dummy';
  159. }
  160. $loader->load($manifest);
  161. } catch (InvalidPackageException $e) {
  162. $errors = array_merge($errors, $e->getErrors());
  163. }
  164. $warnings = array_merge($warnings, $loader->getWarnings());
  165. return array($errors, $publishErrors, $warnings);
  166. }
  167. }