ConfigValidator.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. // strip proprietary since it's not a valid SPDX identifier, but is accepted by composer
  68. if (is_array($manifest['license'])) {
  69. foreach ($manifest['license'] as $key => $license) {
  70. if ('proprietary' === $license) {
  71. unset($manifest['license'][$key]);
  72. }
  73. }
  74. }
  75. $licenseValidator = new SpdxLicenses();
  76. if ('proprietary' !== $manifest['license'] && array() !== $manifest['license'] && !$licenseValidator->validate($manifest['license']) && $licenseValidator->validate(trim($manifest['license']))) {
  77. $warnings[] = sprintf(
  78. 'License %s must not contain extra spaces, make sure to trim it.',
  79. json_encode($manifest['license'])
  80. );
  81. } else if ('proprietary' !== $manifest['license'] && array() !== $manifest['license'] && !$licenseValidator->validate($manifest['license'])) {
  82. $warnings[] = sprintf(
  83. 'License %s is not a valid SPDX license identifier, see https://spdx.org/licenses/ if you use an open license.'
  84. . PHP_EOL .
  85. 'If the software is closed-source, you may use "proprietary" as license.',
  86. json_encode($manifest['license'])
  87. );
  88. }
  89. } else {
  90. $warnings[] = 'No license specified, it is recommended to do so. For closed-source software you may use "proprietary" as license.';
  91. }
  92. if (isset($manifest['version'])) {
  93. $warnings[] = 'The version field is present, it is recommended to leave it out if the package is published on Packagist.';
  94. }
  95. if (!empty($manifest['name']) && preg_match('{[A-Z]}', $manifest['name'])) {
  96. $suggestName = preg_replace('{(?:([a-z])([A-Z])|([A-Z])([A-Z][a-z]))}', '\\1\\3-\\2\\4', $manifest['name']);
  97. $suggestName = strtolower($suggestName);
  98. $publishErrors[] = sprintf(
  99. '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.',
  100. $manifest['name'],
  101. $suggestName
  102. );
  103. }
  104. if (!empty($manifest['type']) && $manifest['type'] == 'composer-installer') {
  105. $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.";
  106. }
  107. // check for require-dev overrides
  108. if (isset($manifest['require']) && isset($manifest['require-dev'])) {
  109. $requireOverrides = array_intersect_key($manifest['require'], $manifest['require-dev']);
  110. if (!empty($requireOverrides)) {
  111. $plural = (count($requireOverrides) > 1) ? 'are' : 'is';
  112. $warnings[] = implode(', ', array_keys($requireOverrides)). " {$plural} required both in require and require-dev, this can lead to unexpected behavior";
  113. }
  114. }
  115. // check for commit references
  116. $require = isset($manifest['require']) ? $manifest['require'] : array();
  117. $requireDev = isset($manifest['require-dev']) ? $manifest['require-dev'] : array();
  118. $packages = array_merge($require, $requireDev);
  119. foreach ($packages as $package => $version) {
  120. if (preg_match('/#/', $version) === 1) {
  121. $warnings[] = sprintf(
  122. 'The package "%s" is pointing to a commit-ref, this is bad practice and can cause unforeseen issues.',
  123. $package
  124. );
  125. }
  126. }
  127. // check for empty psr-0/psr-4 namespace prefixes
  128. if (isset($manifest['autoload']['psr-0'][''])) {
  129. $warnings[] = "Defining autoload.psr-0 with an empty namespace prefix is a bad idea for performance";
  130. }
  131. if (isset($manifest['autoload']['psr-4'][''])) {
  132. $warnings[] = "Defining autoload.psr-4 with an empty namespace prefix is a bad idea for performance";
  133. }
  134. try {
  135. $loader = new ValidatingArrayLoader(new ArrayLoader(), true, null, $arrayLoaderValidationFlags);
  136. if (!isset($manifest['version'])) {
  137. $manifest['version'] = '1.0.0';
  138. }
  139. if (!isset($manifest['name'])) {
  140. $manifest['name'] = 'dummy/dummy';
  141. }
  142. $loader->load($manifest);
  143. } catch (InvalidPackageException $e) {
  144. $errors = array_merge($errors, $e->getErrors());
  145. }
  146. $warnings = array_merge($warnings, $loader->getWarnings());
  147. return array($errors, $publishErrors, $warnings);
  148. }
  149. }