From 5681045553db0794e481938eb6ed8c00d977a5dd Mon Sep 17 00:00:00 2001 From: Bastian Hofmann Date: Tue, 15 Dec 2015 11:47:18 +0100 Subject: [PATCH] Fix weird regex bug when a heredoc identifier starts with an _ --- src/rg/tools/phpnsc/ClassScanner.php | 20 +++--- test/rg/tools/phpnsc/ClassScannerTest.php | 74 ++++++++++++++++++++--- 2 files changed, 76 insertions(+), 18 deletions(-) diff --git a/src/rg/tools/phpnsc/ClassScanner.php b/src/rg/tools/phpnsc/ClassScanner.php index bb603cb..26806d2 100644 --- a/src/rg/tools/phpnsc/ClassScanner.php +++ b/src/rg/tools/phpnsc/ClassScanner.php @@ -88,14 +88,15 @@ private function cleanContent($fileContent) { $fileContent = str_replace('\\\'', ' ', $fileContent); $fileContent = str_replace('\\"', ' ', $fileContent); $fileContent = preg_replace("/([a-zA-Z])\'([a-zA-Z])/", '$1$2', $fileContent); - $cleanWithWhitespaces = function($pattern, $fileContent) { - $getWhitespaces = function($count) { - $s = ''; - for ($i = 0; $i < $count; $i++) { - $s .= ' '; - } - return $s; - }; + $getWhitespaces = function ($count) { + $s = ''; + for ($i = 0; $i < $count; $i++) { + $s .= ' '; + } + return $s; + }; + + $cleanWithWhitespaces = function($pattern, $fileContent) use ($getWhitespaces) { $matches = array(); preg_match_all($pattern, $fileContent, $matches, PREG_OFFSET_CAPTURE); if (isset($matches[1])) { @@ -115,7 +116,8 @@ private function cleanContent($fileContent) { $fileContent = $cleanWithWhitespaces("/(\'.*\')/sU", $fileContent); $fileContent = $cleanWithWhitespaces("/(\".*\")/sU", $fileContent); $fileContent = $cleanWithWhitespaces("/(\/\/.*)/", $fileContent); - $fileContent = $cleanWithWhitespaces("/(<<<(?[A-Za-z_]+).*\k;)/sU", $fileContent); + $fileContent = $cleanWithWhitespaces("/(<<<(?P_[A-Za-z]+).*(?P=tag);)/sU", $fileContent); + $fileContent = $cleanWithWhitespaces("/(<<<(?P[A-Za-z_]+).*(?P=tag);)/sU", $fileContent); if (false) { $fileContent = preg_replace("/(\/\*.*\*\/)/sU", '', $fileContent); diff --git a/test/rg/tools/phpnsc/ClassScannerTest.php b/test/rg/tools/phpnsc/ClassScannerTest.php index 6e26c05..536c441 100644 --- a/test/rg/tools/phpnsc/ClassScannerTest.php +++ b/test/rg/tools/phpnsc/ClassScannerTest.php @@ -123,9 +123,19 @@ class ClassOne implements Bar extends Foo e)throw"Invalid Site ID";if("number"==typeof i&&i%1==0&&i>=0&&(this.timeoutDelay=i),this.siteID=e,this.impressions=[],this._parseFnName=void 0,top===self?(this.sitePage=location.href,this.topframe=1):(this.sitePage=document.referrer,this.topframe=0),"undefined"!=typeof t){if("function"!=typeof t)throw"Invalid jsonp target function";this._parseFnName="cygnus_index_args.parseFn"}"undefined"==typeof _IndexRequestData.requestCounter?_IndexRequestData.requestCounter=Math.floor(256*Math.random()):_IndexRequestData.requestCounter=(_IndexRequestData.requestCounter+1)%256,this.requestID=String((new Date).getTime()%2592e3*256+_IndexRequestData.requestCounter+256),this.initialized=!0}if(cygnus_index_primary_request){for(var s=[],n=0;n0&&(e+=\',"ext": {\'+n.join()+"}"),e+=i+1==this.impressions.length?"}":"},"}return e+="]}"},i.prototype.setPageOverride=function(e){return"string"!=typeof e||e.match(/^\\s*$/)?!1:(this.sitePage=e,!0)},i.prototype.addImpression=function(e,t,i,s,n,r){var u={id:String(this.impressions.length+1)};if("number"!=typeof e||1>=e)return null;if("number"!=typeof t||1>=t)return null;if(("string"==typeof n||"number"==typeof n)&&String(n).length<=50&&(u.slotID=String(n)),u.w=e,u.h=t,void 0!=i&&"number"!=typeof i)return null;if("number"==typeof i){if(0>i)return null;if(u.bidfloor=i,void 0!=s&&"string"!=typeof s)return null;u.bidfloorcur=s}if("undefined"!=typeof r){if(!("number"==typeof r&&r%1===0&&r>=0))return null;u.siteID=r}return this.impressions.push(u),u.id},i.prototype.buildRequest=function(){if(0!=this.impressions.length&&this.initialized===!0){var e=encodeURIComponent(this.serialize()),t="https:"===window.location.protocol?"https://as.casalemedia.com":"http://as.casalemedia.com";return t+="/headertag?v=9&x3=1&fn=cygnus_index_parse_res&s="+this.siteID+"&r="+e,"number"==typeof this.timeoutDelay&&this.timeoutDelay%1==0&&this.timeoutDelay>=0&&(t+="&t="+this.timeoutDelay),t}};try{if("undefined"==typeof cygnus_index_args||"undefined"==typeof cygnus_index_args.siteID||"undefined"==typeof cygnus_index_args.slots)return;"undefined"==typeof _IndexRequestData&&(_IndexRequestData={},_IndexRequestData.impIDToSlotID={},_IndexRequestData.reqOptions={});var d=new i(cygnus_index_args.siteID,cygnus_index_args.parseFn,cygnus_index_args.timeout);cygnus_index_args.url&&"string"==typeof cygnus_index_args.url&&d.setPageOverride(cygnus_index_args.url),_IndexRequestData.impIDToSlotID[d.requestID]={},_IndexRequestData.reqOptions[d.requestID]={};for(var f,g,s=0;s array(3), ), '/root/folder/namespace/ClassTwo.php' => array( - 'ClassOne' => array(8), - 'ClassTwo' => array(9,12), - 'ClassThree' => array(8), - 'OutOfNamespace' => array(12), - 'TypeHintClass' => array(8), - 'OtherNamespace' => array(12), + 'ClassOne' => array(18), + 'ClassTwo' => array(19,22), + 'ClassThree' => array(18), + 'OutOfNamespace' => array(22), + 'TypeHintClass' => array(18), + 'OtherNamespace' => array(22), 'Bar' => array(3), ), ); @@ -158,6 +168,52 @@ interface InterfaceTwo $this->assertEquals($expectedEntities[$file], $this->classScanner->getUsedEntities($file)); } } + + public function testParseUsedEntitiesOtherHeredoc() { + $this->filesystem->filesystem = [ + '/root/folder/namespace/ClassOne.php' => ' + ' +filesystem->filesystem); + + $this->classScanner->parseFilesForClassesAndInterfaces($files); + + $expectedEntities = [ + '/root/folder/namespace/ClassOne.php' => [ + 'Foo' => [3], + 'Bar' => [3], + ], + '/root/folder/namespace/ClassTwo.php' => [ + 'ClassOne' => [8], + 'ClassTwo' => [9, 12], + 'ClassThree' => [8], + 'OutOfNamespace' => [12], + 'TypeHintClass' => [8], + 'OtherNamespace' => [12], + 'Bar' => [3], + ], + ]; + + foreach ($files as $file) { + $this->assertEquals($expectedEntities[$file], $this->classScanner->getUsedEntities($file)); + } + } } class ClassScannerFilesystemMock extends \rg\tools\phpnsc\FilesystemAccess