PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/phplinter/Lint/LMethod.php

http://github.com/robotis/PHPLinter
PHP | 123 lines | 77 code | 7 blank | 39 comment | 19 complexity | 3aa1026cd0beb2430e129d9a6ed48f38 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. ----------------------------------------------------------------------+
  4. * @desc Lint a method.
  5. ----------------------------------------------------------------------+
  6. * @file Lint_method.php
  7. * @author Jóhann T. Maríusson <jtm@robot.is>
  8. * @since Oct 29, 2011
  9. * @package PHPLinter
  10. * @copyright
  11. * phplinter is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation, either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. ----------------------------------------------------------------------+
  24. */
  25. namespace phplinter\Lint;
  26. class LMethod extends BaseLint implements ILint {
  27. /**
  28. ----------------------------------------------------------------------+
  29. * @desc analyse node
  30. * @return Array Reports
  31. ----------------------------------------------------------------------+
  32. */
  33. public function _lint() {
  34. $this->add_parent_data($this->node->name, T_METHOD);
  35. $this->process_tokens();
  36. if($this->node->empty && !$this->node->abstract)
  37. $this->report('WAR_EMPTY_METHOD');
  38. if(!$this->node->dochead)
  39. $this->report('DOC_NO_DOCHEAD_METHOD');
  40. if(!$this->node->visibility)
  41. $this->report('CON_NO_VISIBILITY');
  42. if(!(substr($this->node->name, 0, 2) == '__')) {
  43. if($this->config->match_rule('CON_METHOD_NAME', $this->node->name)) {
  44. $this->report('CON_METHOD_NAME', $this->node->name);
  45. }
  46. }
  47. return $this->reports;
  48. }
  49. /**
  50. ----------------------------------------------------------------------+
  51. * @desc Process token array
  52. ----------------------------------------------------------------------+
  53. */
  54. protected function process_tokens() {
  55. $tcnt = $this->node->token_count;
  56. $et = $this->node->tokens;
  57. $args = false;
  58. $_locals = array();
  59. for($i = 0;$i < $tcnt;$i++) {
  60. switch($et[$i][0]) {
  61. case T_PARENTHESIS_OPEN:
  62. if($args === false) {
  63. $args = $this->parse_args($i);
  64. }
  65. break;
  66. case T_VARIABLE:
  67. if($et[$i][1] == '$this') {
  68. $this->parent_local($i);
  69. } else {
  70. $_locals[] = $et[$i][1];
  71. }
  72. break;
  73. default:
  74. $this->common_tokens($i);
  75. break;
  76. }
  77. }
  78. $locals = array_unique($_locals);
  79. $compares = array(
  80. 'REF_ARGUMENTS' => count($args),
  81. 'REF_LOCALS' => count($locals),
  82. 'REF_BRANCHES' => $this->branches,
  83. 'REF_METHOD_LENGTH' => $this->node->length
  84. );
  85. foreach($compares as $k => $_) {
  86. if($this->config->match_rule($k, $_)) {
  87. $this->report($k, $_);
  88. }
  89. }
  90. if(!$this->node->abstract)
  91. $this->process_args($locals, $args);
  92. $this->process_locals($locals, $_locals, $args);
  93. }
  94. /**
  95. ----------------------------------------------------------------------+
  96. * @desc Process $this token
  97. ----------------------------------------------------------------------+
  98. */
  99. protected function parent_local(&$pos) {
  100. $o = $this->node->tokens;
  101. $j = $this->find($pos, T_STRING);
  102. if($j !== false) {
  103. $k = $o[$this->next($j+1)][0];
  104. if($k === T_PARENTHESIS_OPEN)
  105. $this->add_parent_data($o[$j][1], T_METHOD);
  106. else {
  107. $name = $o[$j][1];
  108. if($this->node->name == '__construct') {
  109. $name = '__construct::' . $name;
  110. }
  111. $this->add_parent_data($name, T_VARIABLE);
  112. }
  113. $pos = $j;
  114. }
  115. }
  116. }