ConfigValidator.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. /**
  19. * Validates a composer configuration.
  20. *
  21. * @author Robert Schönthal <seroscho@googlemail.com>
  22. * @author Jordi Boggiano <j.boggiano@seld.be>
  23. */
  24. class ConfigValidator
  25. {
  26. private $io;
  27. public function __construct(IOInterface $io)
  28. {
  29. $this->io = $io;
  30. }
  31. /**
  32. * Validates the config, and returns the result.
  33. *
  34. * @param string $file The path to the file
  35. * @param integer $arrayLoaderValidationFlags Flags for ArrayLoader validation
  36. *
  37. * @return array a triple containing the errors, publishable errors, and warnings
  38. */
  39. public function validate($file, $arrayLoaderValidationFlags = ValidatingArrayLoader::CHECK_ALL)
  40. {
  41. $errors = array();
  42. $publishErrors = array();
  43. $warnings = array();
  44. // validate json schema
  45. $laxValid = false;
  46. try {
  47. $json = new JsonFile($file, new RemoteFilesystem($this->io));
  48. $manifest = $json->read();
  49. $json->validateSchema(JsonFile::LAX_SCHEMA);
  50. $laxValid = true;
  51. $json->validateSchema();
  52. } catch (JsonValidationException $e) {
  53. foreach ($e->getErrors() as $message) {
  54. if ($laxValid) {
  55. $publishErrors[] = $message;
  56. } else {
  57. $errors[] = $message;
  58. }
  59. }
  60. } catch (\Exception $e) {
  61. $errors[] = $e->getMessage();
  62. return array($errors, $publishErrors, $warnings);
  63. }
  64. // validate actual data
  65. if (!empty($manifest['license'])) {
  66. // strip proprietary since it's not a valid SPDX identifier, but is accepted by composer
  67. if (is_array($manifest['license'])) {
  68. foreach ($manifest['license'] as $key => $license) {
  69. if ('proprietary' === $license) {
  70. unset($manifest['license'][$key]);
  71. }
  72. }
  73. }
  74. $licenseValidator = new SpdxLicense();
  75. if ('proprietary' !== $manifest['license'] && array() !== $manifest['license'] && !$licenseValidator->validate($manifest['license'])) {
  76. $warnings[] = sprintf(
  77. 'License %s is not a valid SPDX license identifier, see http://www.spdx.org/licenses/ if you use an open license.'
  78. ."\nIf the software is closed-source, you may use \"proprietary\" as license.",
  79. json_encode($manifest['license'])
  80. );
  81. }
  82. } else {
  83. $warnings[] = 'No license specified, it is recommended to do so. For closed-source software you may use "proprietary" as license.';
  84. }
  85. if (isset($manifest['version'])) {
  86. $warnings[] = 'The version field is present, it is recommended to leave it out if the package is published on Packagist.';
  87. }
  88. if (!empty($manifest['name']) && preg_match('{[A-Z]}', $manifest['name'])) {
  89. $suggestName = preg_replace('{(?:([a-z])([A-Z])|([A-Z])([A-Z][a-z]))}', '\\1\\3-\\2\\4', $manifest['name']);
  90. $suggestName = strtolower($suggestName);
  91. $warnings[] = sprintf(
  92. '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.',
  93. $manifest['name'],
  94. $suggestName
  95. );
  96. }
  97. if (!empty($manifest['type']) && $manifest['type'] == 'composer-installer') {
  98. $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.";
  99. }
  100. // check for require-dev overrides
  101. if (isset($manifest['require']) && isset($manifest['require-dev'])) {
  102. $requireOverrides = array_intersect_key($manifest['require'], $manifest['require-dev']);
  103. if (!empty($requireOverrides)) {
  104. $plural = (count($requireOverrides) > 1) ? 'are' : 'is';
  105. $warnings[] = implode(', ', array_keys($requireOverrides)). " {$plural} required both in require and require-dev, this can lead to unexpected behavior";
  106. }
  107. }
  108. try {
  109. $loader = new ValidatingArrayLoader(new ArrayLoader(), true, null, $arrayLoaderValidationFlags);
  110. if (!isset($manifest['version'])) {
  111. $manifest['version'] = '1.0.0';
  112. }
  113. if (!isset($manifest['name'])) {
  114. $manifest['name'] = 'dummy/dummy';
  115. }
  116. $loader->load($manifest);
  117. } catch (InvalidPackageException $e) {
  118. $errors = array_merge($errors, $e->getErrors());
  119. }
  120. $warnings = array_merge($warnings, $loader->getWarnings());
  121. return array($errors, $publishErrors, $warnings);
  122. }
  123. }