virtual_enumerations_transformer.php | ||||
|
||||
Coverage | ||||||||||
Classes | Functions / Methods | Lines | ||||||||
Total |
|
0.00% | 0 / 1 |
|
0.00% | 0 / 5 | CRAP |
|
0.00% | 0 / 49 |
constant_name_sorter($a, $b) |
|
0.00% | 0 / 1 | 0 |
|
0.00% | 0 / 2 | |||
VirtualEnumerationsTransformer |
|
0.00% | 0 / 1 |
|
0.00% | 0 / 5 | 272 |
|
0.00% | 0 / 47 |
__construct() |
|
0.00% | 0 / 1 | 2 |
|
0.00% | 0 / 1 | |||
transform(&$parser_model) |
|
0.00% | 0 / 1 | 12 |
|
0.00% | 0 / 6 | |||
processConstants(ParserFile $file) |
|
0.00% | 0 / 1 | 90 |
|
0.00% | 0 / 33 | |||
createVirtualEnum($name) |
|
0.00% | 0 / 1 | 2 |
|
0.00% | 0 / 4 | |||
numSimilarChars($a, $b) |
|
0.00% | 0 / 1 | 6 |
|
0.00% | 0 / 3 |
1 : <?php 2 : /* 3 : Copyright 2008 Josh Heidenreich 4 : 5 : This file is part of Pelzini. 6 : 7 : Pelzini is free software: you can redistribute it and/or modify 8 : it under the terms of the GNU General Public License as published by 9 : the Free Software Foundation, either version 3 of the License, or 10 : (at your option) any later version. 11 : 12 : Pelzini is distributed in the hope that it will be useful, 13 : but WITHOUT ANY WARRANTY; without even the implied warranty of 14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 : GNU General Public License for more details. 16 : 17 : You should have received a copy of the GNU General Public License 18 : along with Pelzini. If not, see <http://www.gnu.org/licenses/>. 19 : */ 20 : 21 : 22 : /** 23 : * Contains the {@link VirtualEnumerationsTransformer} class 24 : * 25 : * @package Transformers 26 : * @author Josh 27 : * @since 0.3 28 : **/ 29 : 30 : /** 31 : * This transformer converts constants that start with the same name into an enumeration of that name. 32 : * 33 : * So if a file has the following constants: 34 : * APP_VERSION 35 : * ITEM_TYPE_APPLE 36 : * ITEM_TYPE_ORANGE 37 : * 38 : * The APPLE and ORANGE constants will become a part of the virtual enumeration ITEM_TYPE. 39 : **/ 40 : class VirtualEnumerationsTransformer extends Transformer { 41 : public function __construct() 42 : { 43 : 44 0 : } 45 : 46 : 47 : /** 48 : * Transforms the data model before outputting. 49 : * 50 : * This transformer converts constants that start with the same name into an enumeration of that name. 51 : * 52 : * @param array $parser_model The data model to transform 53 : * @return array The new data model, or null if there was an error 54 : **/ 55 : public function transform(&$parser_model) 56 : { 57 0 : foreach ($parser_model as $item) { 58 0 : if ($item instanceof ParserFile) { 59 0 : $this->processConstants ($item); 60 0 : } 61 0 : } 62 : 63 0 : return $parser_model; 64 : } 65 : 66 : 67 : /** 68 : * Processes constants for a specified file 69 : **/ 70 : private function processConstants(ParserFile $file) 71 : { 72 0 : usort($file->constants, 'constant_name_sorter'); 73 : 74 0 : reset($file->constants); 75 0 : list ($last_const_id, $last_const) = each($file->constants); 76 : 77 0 : $enum = null; 78 0 : while (list ($constant_id, $constant) = each($file->constants)) { 79 0 : if ($enum) { 80 : // If the constant starts with the same stuff as the current enum, add it 81 0 : if (strncmp($enum->name, $constant->name, strlen($enum->name)) == 0) { 82 0 : $enum->constants[] = $constant; 83 0 : unset ($file->constants[$constant_id]); 84 : 85 0 : } else { 86 : // It doesn't match, so do a check that the constand begins with mostly 87 : // the same stuff - it can have the last two chars wrong. 88 : // If it is close, the enum is shrunk to what matches 89 0 : $num = $this->numSimilarChars($enum->name, $constant->name); 90 0 : if ($num >= 3 and $num >= (strlen($enum->name) - 2)) { 91 0 : if ($constant->name[$num - 1] == '_') $num--; 92 0 : $enum->name = substr($constant->name, 0, $num); 93 0 : $enum->constants[] = $constant; 94 0 : unset ($file->constants[$constant_id]); 95 : 96 : // Of course if there is no match, we cannot force a match. 97 0 : } else { 98 0 : $enum = null; 99 : } 100 : } 101 : 102 0 : } else { 103 : // There is no current enum, so check if one can be created by looking at the 104 : // last constant and the current constant 105 0 : $num = $this->numSimilarChars($last_const->name, $constant->name); 106 0 : if ($num >= 3) { 107 0 : if ($constant->name[$num - 1] == '_') $num--; 108 0 : $enum = $this->createVirtualEnum (substr($constant->name, 0, $num)); 109 0 : $file->enumerations[] = $enum; 110 0 : $enum->constants[] = $last_const; 111 0 : $enum->constants[] = $constant; 112 0 : unset ($file->constants[$last_const_id]); 113 0 : unset ($file->constants[$constant_id]); 114 0 : } 115 : } 116 : 117 0 : $last_const = $constant; 118 0 : $last_const_id = $constant_id; 119 0 : } 120 0 : } 121 : 122 : 123 : /** 124 : * Creates an enum 125 : **/ 126 : private function createVirtualEnum($name) 127 : { 128 0 : $enum = new ParserEnumeration(); 129 0 : $enum->name = $name; 130 0 : $enum->virtual = 1; 131 0 : return $enum; 132 : } 133 : 134 : 135 : /** 136 : * Returns the number of chars in $a that are the same as the chars in $b 137 : **/ 138 : private function numSimilarChars($a, $b) 139 : { 140 0 : $i = 0; 141 : 142 0 : while ($a[$i] == $b[$i]) $i++; 143 : 144 0 : return $i; 145 : } 146 : 147 : 148 : } 149 : 150 : 151 0 : function constant_name_sorter($a, $b) 152 : { 153 0 : return strcasecmp($a->name, $b->name); 154 : } 155 : 156 : 157 : ?> |
Generated by PHP_CodeCoverage 1.1.2 using PHP 5.4.39-0+deb7u2 and PHPUnit 3.6.10 at Fri Sep 11 11:35:19 WIT 2015. |