public function testCachedCallSitesAreRemoved()
 {
     $cachedFooFunc = new GlobalFunction('foo');
     $cachedBarFunc = new GlobalFunction('bar');
     $cachedBarClass = new Clazz('Bar');
     $cachedBarClass->addMethod($cachedBarMethod = new Method('bar'));
     $cachedFooClass = new Clazz('Foo');
     $cachedFooClass->addMethod($cachedFooMethod = new Method('foo'));
     CallSite::create($cachedFooFunc, $cachedBarFunc);
     CallSite::create($cachedFooMethod, $cachedBarFunc);
     CallSite::create($cachedFooMethod, $cachedBarMethod);
     CallSite::create($cachedBarFunc, $cachedFooFunc);
     CallSite::create($cachedBarMethod, $cachedFooFunc);
     $this->provider->addFunction($cachedFooFunc);
     $this->provider->addFunction($cachedBarFunc);
     $this->provider->addClass($cachedBarClass);
     $this->provider->addClass($cachedFooClass);
     $this->analyzeAst('CallGraph/cache.php');
     $fooFunc = $this->registry->getFunction('foo');
     $fooMethod = $this->registry->getClass('Foo')->getMethod('foo')->getMethod();
     $this->assertInCallSites($fooFunc, array($cachedBarMethod), array($cachedBarFunc));
     $this->assertOutCallSites($fooFunc, array(), array());
     $this->assertInCallSites($fooMethod, array(), array());
     $this->assertOutCallSites($fooMethod, array($cachedBarMethod), array($cachedBarFunc));
     $this->assertInCallSites($cachedBarMethod, array($fooMethod), array());
     $this->assertOutCallSites($cachedBarMethod, array(), array($fooFunc));
     $this->assertInCallSites($cachedBarFunc, array($fooMethod), array());
     $this->assertOutCallSites($cachedBarFunc, array(), array($fooFunc));
     $this->assertInCallSites($cachedFooFunc, array($cachedBarMethod), array($cachedBarFunc));
     $this->assertOutCallSites($cachedFooFunc, array(), array($cachedBarFunc));
     $this->assertInCallSites($cachedFooMethod, array(), array());
     $this->assertOutCallSites($cachedFooMethod, array($cachedBarMethod), array($cachedBarFunc));
 }
 public function testPersist()
 {
     $this->packageVersion->addContainer($foo = new Clazz('Foo'));
     $foo->addImplementedInterface('Iterator');
     $foo->addMethod($fooMethod = new Method('foo'));
     $this->packageVersion->addFunction($count_foo = new GlobalFunction('count_foo'));
     CallSite::create($fooMethod, $count_foo);
     $this->persister->persist($this->packageVersion);
     $reloadedFoo = $this->em->createQuery('SELECT c FROM Scrutinizer\\PhpAnalyzer\\Model\\Clazz c WHERE c.name = :name')->setParameter('name', 'Foo')->getSingleResult();
     assert($reloadedFoo instanceof Clazz);
     $this->assertEquals(array('Iterator'), $reloadedFoo->getImplementedInterfaces());
     $this->assertTrue($reloadedFoo->hasMethod('foo'));
     $reloadedCountFoo = $this->em->createQuery('SELECT f FROM Scrutinizer\\PhpAnalyzer\\Model\\GlobalFunction f WHERE f.name = :name')->setParameter('name', 'count_foo')->getSingleResult();
     $this->assertCount(1, $inSites = $reloadedCountFoo->getInCallSites());
     $this->assertSame($reloadedFoo->getMethod('foo')->getMethod(), $inSites[0]->getSource());
 }
 private function copyInCallSites(AbstractFunction $function, AbstractFunction $nonCachedFunction)
 {
     foreach ($function->getInCallSites() as $site) {
         $source = $site->getSource();
         $args = $site->getArgs()->getValues();
         $args = array_map(function ($arg) {
             return clone $arg;
         }, $args);
         $source->removeCallSite($site);
         CallSite::create($source, $nonCachedFunction, $args);
     }
 }