ConfigValidator.php 7.8 KB

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