PageRenderTime 41ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/DocBlox/Transformer/Behaviour/Inherit.php

https://github.com/androa/Docblox
PHP | 141 lines | 42 code | 7 blank | 92 comment | 1 complexity | 207bb148ea93cc2ff7801d6ceab8a66b MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * DocBlox
  4. *
  5. * PHP Version 5
  6. *
  7. * @category DocBlox
  8. * @package Transformer
  9. * @subpackage Behaviour
  10. * @author Mike van Riel <mike.vanriel@naenius.com>
  11. * @copyright 2010-2011 Mike van Riel / Naenius (http://www.naenius.com)
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT
  13. * @link http://docblox-project.org
  14. */
  15. /**
  16. * Behaviour that runs through all elements and inherit base information
  17. * when necessary.
  18. *
  19. * @category DocBlox
  20. * @package Transformer
  21. * @subpackage Behaviour
  22. * @author Mike van Riel <mike.vanriel@naenius.com>
  23. * @license http://www.opensource.org/licenses/mit-license.php MIT
  24. * @link http://docblox-project.org
  25. */
  26. class DocBlox_Transformer_Behaviour_Inherit implements
  27. DocBlox_Transformer_Behaviour_Interface
  28. {
  29. /** @var DocBlox_Core_Log */
  30. protected $logger = null;
  31. /**
  32. * Sets the logger for this behaviour.
  33. *
  34. * @param DocBlox_Core_Log $log
  35. *
  36. * @return void
  37. */
  38. public function setLogger(DocBlox_Core_Log $log = null)
  39. {
  40. $this->logger = $log;
  41. }
  42. /**
  43. * Apply inheritance of docblock elements to all elements.
  44. *
  45. * Apply the inheritance rules from root node to edge leaf; this way the
  46. * inheritance cascades.
  47. *
  48. * Note: the process below must _first_ be done on interfaces and a second
  49. * pass on classes. If this is not done then not everything will be picked
  50. * up because you effectively have 2 separate sets of root nodes.
  51. *
  52. * This does mean that an interface will populate any class in which it is
  53. * implemented but will not walk further down the tree.
  54. *
  55. * Interfaces do not check whether they implement another interface because
  56. * interfaces do not support the IMPLEMENTS keyword.
  57. *
  58. * Actions:
  59. *
  60. * 1. Get root nodes with present leafs
  61. * 2. Get Extended/implemented leafs
  62. * 3. If SD misses for leaf; apply SD of root
  63. * 4. If LD misses for leaf; apply LD of root
  64. * 5. if LD of leaf contains {@inheritdoc}; replace with LD of root
  65. * 6. if @category of leaf is missing; use @category of root
  66. * 7. if @package of leaf is missing; use @package of root
  67. * 8. if @subcategory of leaf is missing; use @subpackage of root
  68. * 9. if @version of leaf is missing; use @version of root
  69. * 10. if @copyright of leaf is missing; use @copyright of root
  70. * 11. if @author of leaf is missing; use @author of root
  71. *
  72. * 12. If root and leaf share a method with the same name:
  73. * 13. If SD misses for leaf method; apply SD of root method
  74. * 14. If LD misses for leaf method; apply LD of root method
  75. * 15. if LD of leaf method contains {@inheritdoc}; replace with LD of root method
  76. * 16. if @params of leaf method is missing; use @params of root method
  77. * 17. if @return of leaf method is missing; use @return of root method
  78. * 18. if @throw/throws of leaf method is missing; use @throws/throw of root method
  79. * 19. if @version of leaf method is missing; use @version of root method
  80. * 20. if @copyright of leaf method is missing; use @copyright of root method
  81. * 21. if @author of leaf method is missing; use @author of root method
  82. *
  83. * 22. If root and leaf share a property with the same name:
  84. * 23. If SD misses for leaf property; apply SD of root property
  85. * 24. If LD misses for leaf property; apply LD of root property
  86. * 25. if LD of leaf property contains {@inheritdoc}; replace with LD of root property
  87. * 26. if @var of leaf property is missing; use @var of root property
  88. * 27. if @version of leaf property is missing; use @version of root property
  89. * 28. if @copyright of leaf property is missing; use @copyright of root property
  90. * 29. if @author of leaf property is missing; use @author of root property
  91. *
  92. * @param DOMDocument $xml
  93. *
  94. * @return DOMDocument
  95. */
  96. public function process(DOMDocument $xml)
  97. {
  98. if ($this->logger) {
  99. $this->logger->log('Adding path information to each xml "file" tag');
  100. }
  101. $xpath = new DOMXPath($xml);
  102. // get all interfaces that do not extend from anything or whose extend
  103. // is not featured in this project; these are considered root nodes.
  104. /** @var DOMElement[] $result */
  105. $result = $xpath->query(
  106. '/project/file/interface[extends=""]' .
  107. '|/project/file/interface[not(extends = /project/file/class/full_name)]'
  108. );
  109. foreach ($result as $node)
  110. {
  111. $inherit = new DocBlox_Transformer_Behaviour_Inherit_Node_Interface(
  112. $node, $xpath
  113. );
  114. $super = array('classes' => array(), 'properties' => array(), 'methods' => array());
  115. $inherit->apply($super, null);
  116. }
  117. // get all classes that do not extend from anything or whose extend
  118. // is not featured in this project; these are considered root nodes.
  119. /** @var DOMElement[] $result */
  120. $result = $xpath->query(
  121. '/project/file/class[extends=""]' .
  122. '|/project/file/class[not(extends = /project/file/class/full_name)]'
  123. );
  124. foreach ($result as $node)
  125. {
  126. $inherit = new DocBlox_Transformer_Behaviour_Inherit_Node_Class(
  127. $node, $xpath
  128. );
  129. $super = array('classes' => array(), 'properties' => array(), 'methods' => array());
  130. $inherit->apply($super, null);
  131. }
  132. return $xml;
  133. }
  134. }