mwdoc-filter.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * Doxygen filter to show correct member variable types in documentation.
  4. *
  5. * Should be set in Doxygen INPUT_FILTER as "php mwdoc-filter.php"
  6. *
  7. * Based on
  8. * <http://virtualtee.blogspot.co.uk/2012/03/tip-for-using-doxygen-for-php-code.html>
  9. *
  10. * Improved to resolve various bugs and better MediaWiki PHPDoc conventions:
  11. *
  12. * - Insert variable name after typehint instead of at end of line so that
  13. * documentation text may follow after "@var Type".
  14. * - Insert typehint into source code before $variable instead of inside the comment
  15. * so that Doxygen interprets it.
  16. * - Strip the text after @var from the output to avoid Doxygen warnings aboug bogus
  17. * symbols being documented but not declared or defined.
  18. *
  19. * Copyright (C) 2012 Tamas Imrei <tamas.imrei@gmail.com> http://virtualtee.blogspot.com/
  20. * Copyright (C) 2015 Timo Tijhof
  21. *
  22. * Permission is hereby granted, free of charge, to any person obtaining
  23. * a copy of this software and associated documentation files (the "Software"),
  24. * to deal in the Software without restriction, including without limitation
  25. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  26. * and/or sell copies of the Software, and to permit persons to whom the
  27. * Software is furnished to do so, subject to the following conditions:
  28. *
  29. * The above copyright notice and this permission notice shall be included
  30. * in all copies or substantial portions of the Software.
  31. *
  32. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  33. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  34. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  35. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  36. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  37. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  38. * DEALINGS IN THE SOFTWARE.
  39. */
  40. if ( PHP_SAPI != 'cli' ) {
  41. die( "This filter can only be run from the command line.\n" );
  42. }
  43. $source = file_get_contents( $argv[1] );
  44. $tokens = token_get_all( $source );
  45. $buffer = $bufferType = null;
  46. foreach ( $tokens as $token ) {
  47. if ( is_string( $token ) ) {
  48. if ( $buffer !== null && $token === ';' ) {
  49. // If we still have a buffer and the statement has ended,
  50. // flush it and move on.
  51. echo $buffer;
  52. $buffer = $bufferType = null;
  53. }
  54. echo $token;
  55. continue;
  56. }
  57. list( $id, $content ) = $token;
  58. switch ( $id ) {
  59. case T_DOC_COMMENT:
  60. // Escape slashes so that references to namespaces are not
  61. // wrongly interpreted as a Doxygen "\command".
  62. $content = addcslashes( $content, '\\' );
  63. // Look for instances of "@var Type" not followed by $name.
  64. if ( preg_match( '#@var\s+([^\s]+)\s+([^\$]+)#s', $content ) ) {
  65. $buffer = preg_replace_callback(
  66. // Strip the "@var Type" part and remember the type
  67. '#(@var\s+)([^\s]+)#s',
  68. function ( $matches ) use ( &$bufferType ) {
  69. $bufferType = $matches[2];
  70. return '';
  71. },
  72. $content
  73. );
  74. } else {
  75. echo $content;
  76. }
  77. break;
  78. case T_VARIABLE:
  79. if ( $buffer !== null ) {
  80. echo $buffer;
  81. echo "$bufferType $content";
  82. $buffer = $bufferType = null;
  83. } else {
  84. echo $content;
  85. }
  86. break;
  87. default:
  88. if ( $buffer !== null ) {
  89. $buffer .= $content;
  90. } else {
  91. echo $content;
  92. }
  93. break;
  94. }
  95. }