PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/Lib/Lints/Lint_function.php

http://github.com/robotis/PHPLinter
PHP | 89 lines | 50 code | 6 blank | 33 comment | 8 complexity | b90756e1cd7c4897c40a48eb0494db5d MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. ----------------------------------------------------------------------+
  4. * @desc Lint a function.
  5. ----------------------------------------------------------------------+
  6. * @file Lint_function.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;
  26. class Lint_function extends BaseLint implements ILint {
  27. /**
  28. ----------------------------------------------------------------------+
  29. * @desc Analyze function
  30. ----------------------------------------------------------------------+
  31. */
  32. public function _lint() {
  33. if($this->element->empty)
  34. $this->report('WAR_EMPTY_FUNCTION');
  35. if(!$this->element->dochead)
  36. $this->report('DOC_NO_DOCHEAD_FUNCTION');
  37. $this->process_tokens();
  38. $regex = $this->rules['CON_FUNCTION_NAME']['compare'];
  39. if(!(substr($this->element->name, 0, 2) == '__')
  40. && !preg_match($regex, $this->element->name))
  41. $this->report('CON_FUNCTION_NAME', $regex);
  42. return $this->reports;
  43. }
  44. /**
  45. ----------------------------------------------------------------------+
  46. * @desc Process tokenstream
  47. ----------------------------------------------------------------------+
  48. */
  49. protected function process_tokens() {
  50. $tcnt = count($this->element->tokens);
  51. $et = $this->element->tokens;
  52. $args = false;
  53. $_locals = array();
  54. $branches = 0;
  55. for($i = 0;$i < $tcnt;$i++) {
  56. switch($et[$i][0]) {
  57. case T_PARENTHESIS_OPEN:
  58. if($args === false) {
  59. $args = $this->parse_args($i);
  60. }
  61. break;
  62. case T_VARIABLE:
  63. $_locals[] = $et[$i][1];
  64. break;
  65. default:
  66. $this->common_tokens($i);
  67. break;
  68. }
  69. }
  70. $locals = array_unique($_locals);
  71. $compares = array(
  72. 'REF_ARGUMENTS' => count($args),
  73. 'REF_LOCALS' => count($locals),
  74. 'REF_BRANCHES' => $this->branches,
  75. 'REF_FUNCTION_LENGTH' => $this->element->length
  76. );
  77. foreach($compares as $k => $_)
  78. if($_ > $this->rules[$k]['compare'])
  79. $this->report($k, $_);
  80. $this->process_args($locals, $args);
  81. $this->process_locals($locals, $_locals, $args);
  82. }
  83. }