Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
91.30% |
21 / 23 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| helpers_PhpTools | |
91.30% |
21 / 23 |
|
0.00% |
0 / 1 |
12.09 | |
0.00% |
0 / 1 |
| getClassInfo | |
91.30% |
21 / 23 |
|
0.00% |
0 / 1 |
12.09 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2014 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | /** |
| 24 | * A utility class handling php language related tasks |
| 25 | */ |
| 26 | class helpers_PhpTools |
| 27 | { |
| 28 | /** |
| 29 | * Returns an array that contains namespace and name of the class defined in the file |
| 30 | * |
| 31 | * Code losely based on http://stackoverflow.com/questions/7153000/get-class-name-from-file |
| 32 | * by user http://stackoverflow.com/users/492901/netcoder |
| 33 | * |
| 34 | * @param string file to anaylse |
| 35 | * @return array |
| 36 | */ |
| 37 | public static function getClassInfo($file) |
| 38 | { |
| 39 | $namespaceToken = PHP_VERSION_ID < 80000 ? T_STRING : T_NAME_QUALIFIED; |
| 40 | |
| 41 | $buffer = file_get_contents($file); |
| 42 | $tokens = @token_get_all($buffer); |
| 43 | $class = $namespace = $buffer = ''; |
| 44 | for ($i = 0; $i < count($tokens); $i++) { |
| 45 | if ($tokens[$i][0] === T_NAMESPACE) { |
| 46 | for ($j = $i + 1; $j < count($tokens); $j++) { |
| 47 | if ($tokens[$j][0] === $namespaceToken) { |
| 48 | $namespace .= '\\' . $tokens[$j][1]; |
| 49 | } elseif ($tokens[$j] === '{' || $tokens[$j] === ';') { |
| 50 | break; |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | if ($tokens[$i][0] === T_CLASS) { |
| 56 | for ($j = $i + 1; $j < count($tokens); $j++) { |
| 57 | if ($tokens[$j] === '{') { |
| 58 | if (!isset($tokens[$i + 2][1])) { |
| 59 | error_log($file . ' does not contain a valid class definition'); |
| 60 | break(2); |
| 61 | } else { |
| 62 | $class = $tokens[$i + 2][1]; |
| 63 | break(2); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | return [ |
| 70 | 'ns' => $namespace, |
| 71 | 'class' => $class |
| 72 | ]; |
| 73 | } |
| 74 | } |