├── .gitignore
├── tests
├── bootstrap.php
└── DocBlockReader
│ └── ReaderTest.php
├── phpunit.xml
├── composer.json
├── composer.lock
├── LICENSE
├── src
└── DocBlockReader
│ └── Reader.php
└── README.md
/.gitignore:
--------------------------------------------------------------------------------
1 | composer.phar
2 | *.sublime-*
3 | test.php
4 | vendor
--------------------------------------------------------------------------------
/tests/bootstrap.php:
--------------------------------------------------------------------------------
1 | add('DocBlockReader\\', __DIR__);
--------------------------------------------------------------------------------
/phpunit.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
147 | array (size=14) 148 | 'var0' => float 1.5 149 | 'var1' => int 1 150 | 'var2' => string '123' (length=3) 151 | 'var3' => string 'abc' (length=3) 152 | 'var4' => 153 | array (size=2) 154 | 0 => string 'a' (length=1) 155 | 1 => string 'b' (length=1) 156 | 'var5' => 157 | array (size=1) 158 | 'x' => string 'y' (length=1) 159 | 'var6' => 160 | array (size=1) 161 | 'x' => 162 | array (size=1) 163 | 'y' => string 'z' (length=1) 164 | 'var7' => 165 | array (size=1) 166 | 'x' => 167 | array (size=1) 168 | 'y' => 169 | array (size=2) 170 | 0 => string 'z' (length=1) 171 | 1 => string 'p' (length=1) 172 | 'var8' => boolean true 173 | 'var9' => null 174 | 'var10' => boolean true 175 | 'var11' => boolean true 176 | 'var12' => boolean false 177 | 'var13' => null 178 |179 | 180 | ### Multi value example 181 | 182 | ```php 183 | getParameters()); 204 | ``` 205 | 206 | will print 207 | 208 | 209 |
210 | array (size=3) 211 | 'var' => string 'x' (length=1) 212 | 'var2' => int 1024 213 | 'param' => 214 | array (size=3) 215 | 0 => string 'string x' (length=8) 216 | 1 => string 'integer y' (length=9) 217 | 2 => string 'array z' (length=7) 218 |219 | 220 | ### Variables on the same line 221 | 222 | ```php 223 | getParameters()); 243 | ``` 244 | 245 | will print 246 | 247 |
248 | array (size=4) 249 | 'get' => boolean true 250 | 'post' => boolean true 251 | 'ajax' => boolean true 252 | 'postParam' => 253 | array (size=3) 254 | 0 => string 'x' (length=1) 255 | 1 => string 'y' (length=1) 256 | 2 => string 'z' (length=1) 257 |258 | 259 | ### Variable declarations functionality example 260 | 261 | I found below functionality useful for filtering `$_GET`/`$_POST` data in CodeIgniter. Hopefully I will soon release my CodeIgniter's modification. 262 | 263 | ```php 264 | getVariableDeclarations("param")); 282 | ``` 283 | 284 | will print 285 | 286 |
287 | array (size=2) 288 | 0 => 289 | array (size=2) 290 | 'type' => string 'string' (length=6) 291 | 'name' => string 'var1' (length=4) 292 | 1 => 293 | array (size=2) 294 | 'type' => string 'integer' (length=7) 295 | 'name' => string 'var2' (length=4) 296 |-------------------------------------------------------------------------------- /tests/DocBlockReader/ReaderTest.php: -------------------------------------------------------------------------------- 1 | commonTest($reader); 60 | } 61 | 62 | public function testPropertyParsing2() { 63 | $reader = new Reader($this, 'myVar2', 'property'); 64 | $x = $reader->getParameter("x"); 65 | $y = $reader->getParameter("y"); 66 | $this->assertSame(1, $x); 67 | $this->assertSame("yes!", $y); 68 | } 69 | 70 | /** 71 | * Issue: https://github.com/jan-swiecki/php-simple-annotations/issues/2 72 | * Thanks to @KrekkieD (https://github.com/KrekkieD) for reporting this issue! 73 | */ 74 | public function testIssue2Problem() { 75 | $reader = new Reader($this, 'issue2', 'property'); 76 | $Lalala = $reader->getParameters()["Lalala"]; 77 | 78 | $this->assertSame(array("somejsonarray", "2", "anotherjsonarray", "3"), $Lalala); 79 | } 80 | 81 | public function testParserOne() 82 | { 83 | $reader = new Reader($this, 'parserFixture'); 84 | $this->commonTest($reader); 85 | } 86 | 87 | public function commonTest($reader) { 88 | $parameters = $reader->getParameters(); 89 | 90 | $this->assertNotEmpty($parameters); 91 | 92 | $this->assertArrayHasKey('number', $parameters); 93 | $this->assertArrayHasKey('string', $parameters); 94 | $this->assertArrayHasKey('array', $parameters); 95 | $this->assertArrayHasKey('object', $parameters); 96 | $this->assertArrayHasKey('nested', $parameters); 97 | $this->assertArrayHasKey('nestedArray', $parameters); 98 | $this->assertArrayHasKey('trueVar', $parameters); 99 | $this->assertArrayHasKey('null-var', $parameters); 100 | $this->assertArrayHasKey('booleanTrue', $parameters); 101 | $this->assertArrayHasKey('booleanFalse', $parameters); 102 | $this->assertArrayHasKey('booleanNull', $parameters); 103 | $this->assertArrayNotHasKey('non_existent_key', $parameters); 104 | 105 | $this->assertSame(1, $parameters['number']); 106 | $this->assertSame("123", $parameters['string']); 107 | $this->assertSame("abc", $parameters['string2']); 108 | $this->assertSame(array("a", "b"), $parameters['array']); 109 | $this->assertSame(array("x" => "y"), $parameters['object']); 110 | $this->assertSame(array("x" => array("y" => "z")), $parameters['nested']); 111 | $this->assertSame(array("x" => array("y" => array("z", "p"))), $parameters['nestedArray']); 112 | $this->assertSame(TRUE, $parameters['trueVar']); 113 | $this->assertSame(NULL, $parameters['null-var']); 114 | 115 | $this->assertSame(TRUE, $parameters['booleanTrue']); 116 | $this->assertSame("tRuE", $parameters['string3']); 117 | $this->assertSame(FALSE, $parameters['booleanFalse']); 118 | $this->assertSame(NULL, $parameters['booleanNull']); 119 | } 120 | 121 | public function testParserOneFromClass() 122 | { 123 | $reader = new Reader($this); 124 | $parameters = $reader->getParameters(); 125 | 126 | $this->assertNotEmpty($parameters); 127 | 128 | $this->assertArrayHasKey('number', $parameters); 129 | $this->assertArrayHasKey('string', $parameters); 130 | $this->assertArrayHasKey('array', $parameters); 131 | $this->assertArrayHasKey('object', $parameters); 132 | $this->assertArrayHasKey('nested', $parameters); 133 | $this->assertArrayHasKey('nestedArray', $parameters); 134 | $this->assertArrayHasKey('trueVar', $parameters); 135 | $this->assertArrayHasKey('null-var', $parameters); 136 | $this->assertArrayHasKey('booleanTrue', $parameters); 137 | $this->assertArrayHasKey('booleanFalse', $parameters); 138 | $this->assertArrayHasKey('booleanNull', $parameters); 139 | $this->assertArrayNotHasKey('non_existent_key', $parameters); 140 | 141 | $this->assertSame(1, $parameters['number']); 142 | $this->assertSame("123", $parameters['string']); 143 | $this->assertSame("abc", $parameters['string2']); 144 | $this->assertSame(array("a", "b"), $parameters['array']); 145 | $this->assertSame(array("x" => "y"), $parameters['object']); 146 | $this->assertSame(array("x" => array("y" => "z")), $parameters['nested']); 147 | $this->assertSame(array("x" => array("y" => array("z", "p"))), $parameters['nestedArray']); 148 | $this->assertSame(TRUE, $parameters['trueVar']); 149 | $this->assertSame(NULL, $parameters['null-var']); 150 | 151 | $this->assertSame(TRUE, $parameters['booleanTrue']); 152 | $this->assertSame("tRuE", $parameters['string3']); 153 | $this->assertSame(FALSE, $parameters['booleanFalse']); 154 | $this->assertSame(NULL, $parameters['booleanNull']); 155 | } 156 | 157 | public function testParserTwo() 158 | { 159 | $reader = new Reader($this, 'parserFixture'); 160 | 161 | $this->assertSame(1, $reader->getParameter('number')); 162 | $this->assertSame("123", $reader->getParameter('string')); 163 | $this->assertSame(array("x" => array("y" => array("z", "p"))), 164 | $reader->getParameter('nestedArray')); 165 | 166 | $this->assertSame(NULL, $reader->getParameter('nullVar')); 167 | $this->assertSame(NULL, $reader->getParameter('null-var')); 168 | $this->assertSame(NULL, $reader->getParameter('non-existent')); 169 | } 170 | 171 | /** 172 | * @number 1 173 | * @string "123" 174 | * @string2 abc 175 | * @array ["a", "b"] 176 | * @object {"x": "y"} 177 | * @nested {"x": {"y": "z"}} 178 | * @nestedArray {"x": {"y": ["z", "p"]}} 179 | * 180 | * @trueVar 181 | * @null-var null 182 | * 183 | * @booleanTrue true 184 | * @string3 tRuE 185 | * @booleanFalse false 186 | * @booleanNull null 187 | * 188 | */ 189 | private function parserFixture() 190 | { 191 | } 192 | 193 | public function testParserEmpty() 194 | { 195 | $reader = new Reader($this, 'parserEmptyFixture'); 196 | $parameters = $reader->getParameters(); 197 | $this->assertSame(array(), $parameters); 198 | } 199 | 200 | private function parserEmptyFixture() 201 | { 202 | } 203 | 204 | public function testParserMulti() 205 | { 206 | $reader = new Reader($this, 'parserMultiFixture'); 207 | $parameters = $reader->getParameters(); 208 | 209 | $this->assertNotEmpty($parameters); 210 | $this->assertArrayHasKey('param', $parameters); 211 | $this->assertArrayHasKey('var', $parameters); 212 | 213 | $this->assertSame("x",$parameters["var"]); 214 | $this->assertSame(1024,$parameters["var2"]); 215 | 216 | $this->assertSame( 217 | array("string x", "integer y", "array z"), 218 | $parameters["param"]); 219 | 220 | } 221 | 222 | /** 223 | * @var x 224 | * @var2 1024 225 | * @param string x 226 | * @param integer y 227 | * @param array z 228 | */ 229 | private function parserMultiFixture() 230 | { 231 | } 232 | 233 | public function testParserThree() 234 | { 235 | $reader = new Reader($this, 'fixtureThree'); 236 | // $allowedRequest = $reader->getParameter("allowedRequest"); 237 | 238 | $postParam = $reader->getParameter("postParam"); 239 | 240 | $this->assertNotEmpty($postParam); 241 | } 242 | 243 | /** 244 | * @allowedRequest ["ajax", "post"] 245 | * @postParam integer orderId 246 | * @postParam array productIds 247 | * @postParam string newValue 248 | */ 249 | private function fixtureThree() 250 | { 251 | 252 | } 253 | 254 | public function testParserFour() 255 | { 256 | $reader = new Reader($this, 'fixtureFour'); 257 | 258 | $this->assertSame(TRUE, $reader->getParameter('get')); 259 | $this->assertSame(TRUE, $reader->getParameter('post')); 260 | $this->assertSame(TRUE, $reader->getParameter('ajax')); 261 | $this->assertSame(array("x","y","z"), $reader->getParameter('postParam')); 262 | } 263 | 264 | public function testParserFourBis() 265 | { 266 | $reader = new Reader($this, 'fixtureFour'); 267 | 268 | $parameters = $reader->getParameters(); 269 | 270 | $this->assertArrayHasKey('get', $parameters); 271 | $this->assertArrayHasKey('post', $parameters); 272 | $this->assertArrayHasKey('ajax', $parameters); 273 | $this->assertArrayHasKey('postParam', $parameters); 274 | 275 | $this->assertSame(TRUE, $parameters['get']); 276 | $this->assertSame(TRUE, $parameters['post']); 277 | $this->assertSame(TRUE, $parameters['ajax']); 278 | $this->assertSame(array("x","y","z"), $parameters['postParam']); 279 | 280 | } 281 | 282 | /** 283 | * @get @post 284 | * @ajax 285 | * @postParam x 286 | * @postParam y 287 | * @postParam z 288 | */ 289 | private function fixtureFour() 290 | { 291 | } 292 | 293 | public function testFive() 294 | { 295 | $reader1 = new Reader($this, 'fixtureFive'); 296 | $reader2 = new Reader($this, 'fixtureFive'); 297 | 298 | $parameters1 = $reader1->getParameters(); 299 | 300 | $trueVar1 = $parameters1['trueVar1']; 301 | 302 | $this->assertSame(TRUE,$trueVar1); 303 | $this->assertSame(TRUE,$reader2->getParameter("trueVar2")); 304 | 305 | } 306 | 307 | /** 308 | * @trueVar1 309 | * @trueVar2 310 | */ 311 | private function fixtureFive() 312 | { 313 | } 314 | 315 | public function testVariableDeclarations() 316 | { 317 | $reader = new Reader($this, 'fixtureVariableDeclarations'); 318 | $declarations = $reader->getVariableDeclarations("param"); 319 | $this->assertNotEmpty($declarations); 320 | 321 | $this->assertSame(array( 322 | array("type"=>"string", "name" => "var1"), 323 | array("type"=>"integer", "name" => "var2") 324 | ), $declarations); 325 | } 326 | 327 | /** 328 | * @param string var1 329 | * @param integer var2 330 | */ 331 | private function fixtureVariableDeclarations() 332 | { 333 | } 334 | 335 | /** 336 | * @dataProvider badVariableDataProvider 337 | * @expectedException InvalidArgumentException 338 | */ 339 | public function testBadVariableDeclarations($methodName) 340 | { 341 | $reader = new Reader($this, $methodName); 342 | $declarations = $reader->getVariableDeclarations("param"); 343 | } 344 | 345 | /** 346 | * @param false 347 | */ 348 | private function fixtureBadVariableDeclarationsOne() 349 | { 350 | } 351 | 352 | /** 353 | * @param true 354 | */ 355 | private function fixtureBadVariableDeclarationsTwo() 356 | { 357 | } 358 | 359 | public function badVariableDataProvider() 360 | { 361 | return array( 362 | array('fixtureBadVariableDeclarationsOne'), 363 | array('fixtureBadVariableDeclarationsTwo') 364 | ); 365 | } 366 | } 367 | --------------------------------------------------------------------------------