InterfaceMethodGenerator.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Code\Generator;
  7. /**
  8. * Interface method generator.
  9. */
  10. class InterfaceMethodGenerator extends \Zend\Code\Generator\MethodGenerator
  11. {
  12. /**
  13. * {@inheritdoc}
  14. */
  15. public function generate()
  16. {
  17. $this->validateMethodModifiers();
  18. $output = '';
  19. if (!$this->getName()) {
  20. return $output;
  21. }
  22. $indent = $this->getIndentation();
  23. if (($docBlock = $this->getDocBlock()) !== null) {
  24. $docBlock->setIndentation($indent);
  25. $output .= $docBlock->generate();
  26. }
  27. $output .= $indent;
  28. $output .= $this->getVisibility() . (($this->isStatic()) ? ' static' : '')
  29. . ' function ' . $this->getName() . '(';
  30. $parameters = $this->getParameters();
  31. if (!empty($parameters)) {
  32. $parameterOutput = [];
  33. foreach ($parameters as $parameter) {
  34. $parameterOutput[] = $parameter->generate();
  35. }
  36. $output .= implode(', ', $parameterOutput);
  37. }
  38. $output .= ');' . self::LINE_FEED;
  39. return $output;
  40. }
  41. /**
  42. * Ensure that used method modifiers are allowed for interface methods.
  43. *
  44. * @throws \LogicException
  45. * @return void
  46. */
  47. protected function validateMethodModifiers()
  48. {
  49. if ($this->getVisibility() != self::VISIBILITY_PUBLIC) {
  50. throw new \LogicException(
  51. "Interface method visibility can only be 'public'. Method name: '{$this->getName()}'"
  52. );
  53. }
  54. if ($this->isFinal()) {
  55. throw new \LogicException(
  56. "Interface method cannot be marked as 'final'. Method name: '{$this->getName()}'"
  57. );
  58. }
  59. if ($this->isAbstract()) {
  60. throw new \LogicException(
  61. "'abstract' modifier cannot be used for interface method. Method name: '{$this->getName()}'"
  62. );
  63. }
  64. }
  65. }