FixerConfigurationResolver.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\FixerConfiguration;
  12. use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
  13. use Symfony\Component\OptionsResolver\OptionsResolver;
  14. final class FixerConfigurationResolver implements FixerConfigurationResolverInterface
  15. {
  16. /**
  17. * @var FixerOptionInterface[]
  18. */
  19. private $options = array();
  20. /**
  21. * @var string[]
  22. */
  23. private $registeredNames = array();
  24. /**
  25. * @param iterable<FixerOptionInterface> $options
  26. */
  27. public function __construct($options)
  28. {
  29. foreach ($options as $option) {
  30. $this->addOption($option);
  31. }
  32. if (empty($this->registeredNames)) {
  33. throw new \LogicException('Options cannot be empty.');
  34. }
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function getOptions()
  40. {
  41. return $this->options;
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function resolve(array $options)
  47. {
  48. $resolver = new OptionsResolver();
  49. foreach ($this->options as $option) {
  50. $name = $option->getName();
  51. if ($option instanceof AliasedFixerOption) {
  52. $alias = $option->getAlias();
  53. if (array_key_exists($alias, $options)) {
  54. // @TODO 2.12 Trigger a deprecation notice and add a test for it
  55. if (array_key_exists($name, $options)) {
  56. throw new InvalidOptionsException(sprintf('Aliased option %s/%s is passed multiple times.', $name, $alias));
  57. }
  58. $options[$name] = $options[$alias];
  59. unset($options[$alias]);
  60. }
  61. }
  62. if ($option->hasDefault()) {
  63. $resolver->setDefault($name, $option->getDefault());
  64. } else {
  65. $resolver->setRequired($name);
  66. }
  67. $allowedValues = $option->getAllowedValues();
  68. if (null !== $allowedValues) {
  69. foreach ($allowedValues as &$allowedValue) {
  70. if (is_object($allowedValue) && is_callable($allowedValue)) {
  71. $allowedValue = function ($values) use ($allowedValue) {
  72. return $allowedValue($values);
  73. };
  74. }
  75. }
  76. $resolver->setAllowedValues($name, $allowedValues);
  77. }
  78. $allowedTypes = $option->getAllowedTypes();
  79. if (null !== $allowedTypes) {
  80. $resolver->setAllowedTypes($name, $allowedTypes);
  81. }
  82. $normalizer = $option->getNormalizer();
  83. if (null !== $normalizer) {
  84. $resolver->setNormalizer($name, $normalizer);
  85. }
  86. }
  87. return $resolver->resolve($options);
  88. }
  89. /**
  90. * @param FixerOptionInterface $option
  91. *
  92. * @throws \LogicException when the option is already defined
  93. *
  94. * @return $this
  95. */
  96. private function addOption(FixerOptionInterface $option)
  97. {
  98. $name = $option->getName();
  99. if (in_array($name, $this->registeredNames, true)) {
  100. throw new \LogicException(sprintf('The "%s" option is defined multiple times.', $name));
  101. }
  102. $this->options[] = $option;
  103. $this->registeredNames[] = $name;
  104. return $this;
  105. }
  106. }