Beispiel #1
0
 /**
  * (non-PHPdoc)
  * @see PHPCPD_Detector_Tokenizer_AbstractTokenizer::cpd()
  */
 public function cpd(PHPCPD_Detector_Strategy $strategy, $file)
 {
     $buffer = file_get_contents($file);
     // As the CodeSniffer-Tokenizer is replacing literal newlines, we're saving them
     $buffer = str_replace('\\n', 'T_EOL_STRING', $buffer);
     $this->_iLines = substr_count($buffer, "\n");
     $aTokens = $currentTokenPositions = array();
     $currentSignature = '';
     $oTokenizer = new PHP_CodeSniffer_Tokenizers_JS();
     $aTokens = $oTokenizer->tokenizeString($buffer);
     $tokenNr = 0;
     $line = 1;
     unset($buffer, $oTokenizer);
     foreach ($aTokens as $token) {
         if (isset($this->tokensIgnoreList[$token['code']]) !== true) {
             $currentTokenPositions[$tokenNr++] = $line;
             $sContent = str_replace('T_EOL_STRING', '\\n', $token['content']);
             $currentSignature .= chr($token['code'] & 255) . pack('N*', crc32($sContent));
         }
         $line += substr_count($token['content'], '\\n');
     }
     unset($aTokens);
     $strategy->tokenFactor($this->_fTokenFactor, $this->_iMinLines)->processFile($file, $currentTokenPositions, $currentSignature);
     return $this;
 }
Beispiel #2
0
 /**
  * (non-PHPdoc)
  * @see PHPCPD_Detector_Tokenizer_AbstractTokenizer::cpd()
  */
 public function cpd(PHPCPD_Detector_Strategy $strategy, $file)
 {
     $buffer = file_get_contents($file);
     $this->_iLines = substr_count($buffer, "\n");
     $aTokens = $currentTokenPositions = array();
     $currentSignature = '';
     $oTokenizer = new PHP_CodeSniffer_Tokenizers_CSS();
     $aTokens = $oTokenizer->tokenizeString($buffer);
     $tokenNr = 0;
     $line = 1;
     unset($buffer, $oTokenizer);
     $bStyle = false;
     foreach ($aTokens as $token) {
         if ($token['code'] === T_OPEN_CURLY_BRACKET) {
             $bStyle = true;
         } elseif ($token['code'] === T_CLOSE_CURLY_BRACKET) {
             $bStyle = false;
         }
         if (isset($this->tokensIgnoreList[$token['code']]) !== true) {
             if ($bStyle === true) {
                 $currentTokenPositions[$tokenNr++] = $line;
                 $currentSignature .= chr($token['code'] & 255) . pack('N*', crc32($token['content']));
             }
         }
         $line += substr_count($token['content'], "\n");
     }
     unset($aTokens);
     $strategy->tokenFactor($this->_fTokenFactor, $this->_iMinLines)->processFile($file, $currentTokenPositions, $currentSignature);
     return $this;
 }
Beispiel #3
0
 /**
  * (non-PHPdoc)
  * @see PHPCPD_Detector_Tokenizer_AbstractTokenizer::cpd()
  */
 public function cpd(PHPCPD_Detector_Strategy $strategy, $file)
 {
     $buffer = file_get_contents($file);
     $this->_iLines = substr_count($buffer, "\n");
     $currentTokenPositions = array();
     $currentSignature = '';
     $tokens = token_get_all($buffer);
     $tokenNr = 0;
     $line = 1;
     unset($buffer);
     foreach ($tokens as $token) {
         if (is_string($token)) {
             $line += substr_count($token, "\n");
         } else {
             if (!isset($this->tokensIgnoreList[$token[0]])) {
                 $currentTokenPositions[$tokenNr++] = $line;
                 $currentSignature .= chr($token[0] & 255) . pack('N*', crc32($token[1]));
             }
             $line += substr_count($token[1], "\n");
         }
     }
     $strategy->tokenFactor($this->_fTokenFactor, $this->_iMinLines)->processFile($file, $currentTokenPositions, $currentSignature);
     return $this;
 }
Beispiel #4
0
 /**
  * Copy & Paste Detection (CPD).
  *
  * @param  Iterator|array   $files     List of files to process
  * @param  integer          $minLines  Minimum number of identical lines
  * @param  integer          $minTokens Minimum number of identical tokens
  */
 public function copyPasteDetection($files, $minLines = 5, $minTokens = 70)
 {
     $oMap = new PHPCPD_CloneMap();
     if ($this->output !== NULL) {
         $bar = new ezcConsoleProgressbar($this->output, count($files));
         print 'Processing files' . PHP_EOL;
     }
     $this->strategy->init($oMap, $minLines, $minTokens);
     $oTokenizer = new PHPCPD_Detector_Tokenizer();
     foreach ($files as $file) {
         $oTokenizer->process($this->strategy, $oMap, $file);
         if ($this->output !== NULL) {
             $bar->advance();
         }
     }
     if ($this->output !== NULL) {
         print PHP_EOL . PHP_EOL;
     }
     return $oMap;
 }
Beispiel #5
0
 /**
  * (non-PHPdoc)
  * @see PHPCPD_Detector_Tokenizer_AbstractTokenizer::cpd()
  */
 public function cpd(PHPCPD_Detector_Strategy $strategy, $file)
 {
     $buffer = file_get_contents($file);
     $this->_iLines = substr_count($buffer, PHP_EOL);
     $this->_aTokens = array();
     $currentTokenPositions = array();
     $currentSignature = '';
     $tidy_config = array('clean' => true, 'drop-proprietary-attributes' => true, 'output-xhtml' => true, 'hide-comments' => true, 'show-body-only' => true, 'word-2000' => true, 'wrap' => '0');
     $oTidy = new tidy($file, $tidy_config);
     $oNode = $oTidy->root();
     $this->_tokenHelper($oNode);
     unset($oTidy, $oNode, $buffer);
     $tokenNr = 0;
     foreach ($this->_aTokens as $token) {
         if (!isset($this->tokensIgnoreList[$token[0]])) {
             $currentTokenPositions[$tokenNr++] = $token[2];
             $currentSignature .= chr($token[0] & 255) . pack('N*', crc32($token[1]));
         }
     }
     $strategy->tokenFactor($this->_fTokenFactor, $this->_iMinLines)->processFile($file, $currentTokenPositions, $currentSignature);
     return $this;
 }