Пример #1
0
 public function testShouldRevertToOriginalStaticMethodBodyWhenRequested()
 {
     $mutationInstance = new OperatorAddition(2);
     $testeeInstance = new Testee();
     $testeeInstance->setFileName($this->root . '/runkit/Math2.php')->setClassName('RunkitTest_Math2')->setMethodName('add')->setArguments('$op1,$op2')->setTokens(array(array(335, 'return', 7), array(309, '$op1', 7), '+', array(309, '$op2', 7), ';'));
     $mutant = new Mutant($testeeInstance, $mutationInstance);
     $runkit = new Runkit();
     $runkit->applyMutation($mutant);
     $runkit->reverseMutation($mutant);
     $this->assertEquals(2, \RunkitTest_Math2::add(1, 1));
 }
Пример #2
0
 /**
  * @param string $path
  * @return array
  */
 public function parseFile($path)
 {
     $tokens = token_get_all(file_get_contents($path));
     $inblock = false;
     $inarg = false;
     $curlycount = 0;
     $roundcount = 0;
     $blockTokens = array();
     $argTokens = array();
     $methods = array();
     $static = false;
     $staticClassCapture = true;
     $namespace = "";
     foreach ($tokens as $index => $token) {
         if (is_array($token) && $token[0] == T_NAMESPACE) {
             $namespace = "\\" . $tokens[$index + 2][1];
             $i = 3;
             while (is_array($tokens[$index + $i]) && ($token[0] = T_NS_SEPARATOR)) {
                 $namespace .= "\\" . $tokens[$index + $i + 1][1];
                 $i += 2;
             }
         }
         if (is_array($token) && $token[0] == T_STATIC && $staticClassCapture === true) {
             $static = true;
             $staticClassCapture = false;
             continue;
         }
         // get class name
         if (is_array($token) && ($token[0] == T_CLASS || $token[0] == T_INTERFACE)) {
             $className = $tokens[$index + 2][1];
             $staticClassCapture = false;
             continue;
         }
         // get method name
         if (is_array($token) && $token[0] == T_FUNCTION && !$inblock) {
             if (!is_array($tokens[$index + 2]) || $tokens[$index + 2][0] != T_STRING || $tokens[$index + 1] == "(") {
                 // probably a closure, skip for now
                 continue;
             }
             $methodName = $tokens[$index + 2][1];
             $inarg = true;
             $roundcount = false;
             $mutable = new ClassMethodTestee();
             $mutable->setFileName($path)->setClassName($className)->setMethodName($methodName);
             continue;
         }
         // Get the method's parameter string
         if ($inarg) {
             if ($token == '(') {
                 $roundcount += 1;
             } elseif ($token == ')') {
                 $roundcount -= 1;
             }
             if ($roundcount == 1 && $token == '(') {
                 continue;
             } elseif ($roundcount >= 1) {
                 $argTokens[] = $token;
             } elseif ($roundcount === 0) {
                 $mutable->setArguments($this->_reconstructFromTokens($argTokens));
                 $argTokens = array();
                 $inarg = false;
                 $inblock = true;
             }
             continue;
         }
         // Get the method's block code
         if ($inblock) {
             if ($token == '{' || is_array($token) && $token[0] == T_CURLY_OPEN) {
                 $curlycount += 1;
             } elseif ($token == '}') {
                 $curlycount -= 1;
             }
             if ($curlycount == 1 && $token == '{') {
                 continue;
             } elseif ($curlycount >= 1) {
                 if (is_array($token) && $token[0] == 370) {
                     continue;
                 }
                 $blockTokens[] = $token;
             } elseif ($curlycount == 0 && count($blockTokens) > 0) {
                 $mutable->setTokens($blockTokens);
                 $methods[] = $mutable;
                 $mutable = array();
                 $blockTokens = array();
                 $inblock = false;
                 $staticClassCapture = true;
             }
         }
     }
     return $methods;
 }