ConfigValidator.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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, new RemoteFilesystem($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'])) {
  77. $warnings[] = sprintf(
  78. 'License %s is not a valid SPDX license identifier, see http://www.spdx.org/licenses/ if you use an open license.'
  79. ."\nIf the software is closed-source, you may use \"proprietary\" as license.",
  80. json_encode($manifest['license'])
  81. );
  82. }
  83. } else {
  84. $warnings[] = 'No license specified, it is recommended to do so. For closed-source software you may use "proprietary" as license.';
  85. }
  86. if (isset($manifest['version'])) {
  87. $warnings[] = 'The version field is present, it is recommended to leave it out if the package is published on Packagist.';
  88. }
  89. if (!empty($manifest['name']) && preg_match('{[A-Z]}', $manifest['name'])) {
  90. $suggestName = preg_replace('{(?:([a-z])([A-Z])|([A-Z])([A-Z][a-z]))}', '\\1\\3-\\2\\4', $manifest['name']);
  91. $suggestName = strtolower($suggestName);
  92. $publishErrors[] = sprintf(
  93. '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.',
  94. $manifest['name'],
  95. $suggestName
  96. );
  97. }
  98. if (!empty($manifest['type']) && $manifest['type'] == 'composer-installer') {
  99. $warnings[] = "The package type 'composer-installer' is deprecated. Please distribute your custom installers as plugins from now on. See http://getcomposer.org/doc/articles/plugins.md for plugin documentation.";
  100. }
  101. // check for require-dev overrides
  102. if (isset($manifest['require']) && isset($manifest['require-dev'])) {
  103. $requireOverrides = array_intersect_key($manifest['require'], $manifest['require-dev']);
  104. if (!empty($requireOverrides)) {
  105. $plural = (count($requireOverrides) > 1) ? 'are' : 'is';
  106. $warnings[] = implode(', ', array_keys($requireOverrides)). " {$plural} required both in require and require-dev, this can lead to unexpected behavior";
  107. }
  108. }
  109. // check for commit references
  110. $require = isset($manifest['require']) ? $manifest['require'] : array();
  111. $requireDev = isset($manifest['require-dev']) ? $manifest['require-dev'] : array();
  112. $packages = array_merge($require, $requireDev);
  113. foreach ($packages as $package => $version) {
  114. if (preg_match('/#/', $version) === 1) {
  115. $warnings[] = sprintf(
  116. 'The package "%s" is pointing to a commit-ref, this is bad practice and can cause unforeseen issues.',
  117. $package
  118. );
  119. }
  120. }
  121. // check for empty psr-0/psr-4 namespace prefixes
  122. if (isset($manifest['autoload']['psr-0'][''])) {
  123. $warnings[] = "Defining autoload.psr-0 with an empty namespace prefix is a bad idea for performance";
  124. }
  125. if (isset($manifest['autoload']['psr-4'][''])) {
  126. $warnings[] = "Defining autoload.psr-4 with an empty namespace prefix is a bad idea for performance";
  127. }
  128. try {
  129. $loader = new ValidatingArrayLoader(new ArrayLoader(), true, null, $arrayLoaderValidationFlags);
  130. if (!isset($manifest['version'])) {
  131. $manifest['version'] = '1.0.0';
  132. }
  133. if (!isset($manifest['name'])) {
  134. $manifest['name'] = 'dummy/dummy';
  135. }
  136. $loader->load($manifest);
  137. } catch (InvalidPackageException $e) {
  138. $errors = array_merge($errors, $e->getErrors());
  139. }
  140. $warnings = array_merge($warnings, $loader->getWarnings());
  141. return array($errors, $publishErrors, $warnings);
  142. }
  143. }