Arbit - project tracking

Static Reflection

History

Diff

268 269 /test/ReflectionClassProxyTest.php
568 568 $subject->expects( $this->once() )
569 569 ->method( $methodName );
570 570
571 - $session = $this->createSession();
572 - $session->expects( $this->once() )
571 + $context = $this->createContext();
572 + $context->expects( $this->once() )
573 573 ->method( 'getClass' )
574 574 ->with( $this->equalTo( __CLASS__ ) )
575 575 ->will( $this->returnValue( $subject ) );
576 576
577 - return new ReflectionClassProxy( $session, __CLASS__ );
577 + return new ReflectionClassProxy( $context, __CLASS__ );
578 578 }
579 579 }
268 269 /test/parser/ParserTest.php
1437 1437 {
1438 1438 $context = $this->createContext();
1439 1439 $context->expects( $this->once() )
1440 - ->method( 'registerClass' );
1440 + ->method( 'addClass' );
1441 1441
1442 1442 $parser = new Parser( $context );
1443 1443 $parser->parseFile( $this->getPathnameForClass( 'ClassWithoutNamespace' ) );
268 269 /test/ReflectionClassProxyContextTest.php
78 78 }
79 79
80 80 /**
81 - * testGetClassReturnsClassInstanceOfPreviousRegisteredClass
81 + * testGetClassReferenceReturnsClassInstanceOfPreviousRegisteredClass
82 82 *
83 83 * @return void
84 84 * @covers \pdepend\reflection\ReflectionClassProxyContext
85 85 * @group reflection
86 86 * @group unittest
87 87 */
88 - public function testGetClassReturnsClassInstanceOfPreviousRegisteredClass()
88 + public function testGetClassReferenceReturnsClassInstanceOfPreviousRegisteredClass()
89 89 {
90 90 $class = new \ReflectionClass( __CLASS__ );
91 91
92 92 $context = new ReflectionClassProxyContext( $this->createSession() );
93 - $context->registerClass( $class );
93 + $context->addClass( $class );
94 94
95 95 $this->assertSame( $class, $context->getClassReference( __CLASS__ ) );
96 96 }
97 +
98 + /**
99 + * testGetClassReferenceHandlesClassNamesCaseInsensitive
100 + *
101 + * @return void
102 + * @covers \pdepend\reflection\ReflectionClassProxyContext
103 + * @group reflection
104 + * @group unittest
105 + */
106 + public function testGetClassReferenceHandlesClassNamesCaseInsensitive()
107 + {
108 + $class = new \ReflectionClass( __CLASS__ );
109 +
110 + $context = new ReflectionClassProxyContext( $this->createSession() );
111 + $context->addClass( $class );
112 +
113 + $this->assertSame( $class, $context->getClassReference( strtoupper( __CLASS__ ) ) );
114 + }
115 +
116 + /**
117 + * testGetClassReturnsClassInstanceOfPreviousRegisteredClass
118 + *
119 + * @return void
120 + * @covers \pdepend\reflection\ReflectionClassProxyContext
121 + * @group reflection
122 + * @group unittest
123 + */
124 + public function testGetClassReturnsClassInstanceOfPreviousRegisteredClass()
125 + {
126 + $class = new \ReflectionClass( __CLASS__ );
127 +
128 + $context = new ReflectionClassProxyContext( $this->createSession() );
129 + $context->addClass( $class );
130 +
131 + $this->assertSame( $class, $context->getClass( __CLASS__ ) );
132 + }
133 +
134 + /**
135 + * testGetClassReferenceHandlesClassNamesCaseInsensitive
136 + *
137 + * @return void
138 + * @covers \pdepend\reflection\ReflectionClassProxyContext
139 + * @group reflection
140 + * @group unittest
141 + */
142 + public function testGetClassHandlesClassNamesCaseInsensitive()
143 + {
144 + $class = new \ReflectionClass( __CLASS__ );
145 +
146 + $session = $this->createSession();
147 + $session->expects( $this->never() )
148 + ->method( 'getClass' );
149 +
150 + $context = new ReflectionClassProxyContext( $session );
151 + $context->addClass( $class );
152 +
153 + $context->getClass( strtoupper( __CLASS__ ) );
154 + }
155 +
156 + /**
157 + * testGetClassDelegatesToSessionWhenMatchingClassExists
158 + *
159 + * @return void
160 + * @covers \pdepend\reflection\ReflectionClassProxyContext
161 + * @group reflection
162 + * @group unittest
163 + */
164 + public function testGetClassDelegatesToSessionWhenMatchingClassExists()
165 + {
166 + $session = $this->createSession();
167 + $session->expects( $this->once() )
168 + ->method( 'getClass' )
169 + ->with( $this->equalTo( __CLASS__ ) );
170 +
171 + $context = new ReflectionClassProxyContext( $session );
172 + $context->getClass( __CLASS__ );
173 + }
97 174 }
268 269 /source/pdepend/reflection/interfaces/ParserContext.php
76 76 function getClassReference( $className );
77 77
78 78 /**
79 + * Returns a previous registered <b>\ReflectionClass</b> instance that
80 + * matches the given class name. Or throws a reflection exception when no
81 + * matching class exists.
82 + *
83 + * @param string $className Full qualified name of the request class.
84 + *
85 + * @return \ReflectionClass
86 + * @throws \ReflectionException When no matching class or interface for the
87 + * given name exists.
88 + */
89 + function getClass( $className );
90 +
91 + /**
79 92 * This method can/should be called by the parser whenever the source of a
80 93 * class or interface has been completed.
81 94 *
84 97 *
85 98 * @return void
86 99 */
87 - function registerClass( \ReflectionClass $class );
100 + function addClass( \ReflectionClass $class );
88 101 }
89 102
90 103 // @codeCoverageIgnoreEnd
268 269 /source/pdepend/reflection/ReflectionSession.php
97 97 public static function createDefaultSession( SourceResolver $resolver )
98 98 {
99 99 $session = new ReflectionSession();
100 +
100 101 $session->addClassFactory( new factories\InternalReflectionClassFactory() );
101 - $session->addClassFactory( new factories\StaticReflectionClassFactory( new ReflectionClassProxyContext( $session ), $resolver ) );
102 + $session->addClassFactory(
103 + new factories\StaticReflectionClassFactory(
104 + new ReflectionClassProxyContext( $session ),
105 + $resolver
106 + )
107 + );
102 108 $session->addClassFactory( new factories\NullReflectionClassFactory() );
103 109
104 110 return $session;
268 269 /source/pdepend/reflection/ReflectionClassProxy.php
48 48
49 49 namespace pdepend\reflection;
50 50
51 +use pdepend\reflection\interfaces\ParserContext;
52 +
51 53 /**
52 54 * Proxy class representing a variable class or interface node within the
53 55 * parsed source.
72 74 *
73 75 * @var \pdepend\reflection\ReflectionSession
74 76 */
75 - private $_session = null;
77 + private $_context = null;
76 78
77 79 /**
78 80 * The qualified class/interface name.
91 93 /**
92 94 * Constructs a new class/interface proxy.
93 95 *
94 - * @param \pdepend\reflection\ReflectionSession $session The currently
95 - * used reflection session instance that is used during the actual
96 + * @param \pdepend\reflection\interfaces\ParserContext $context The currently
97 + * used parser context instance that is used during the actual
96 98 * parsing process.
97 - * @param string $name Qualified name
99 + * @param string $name Qualified name
98 100 * of the class/interface that is proxied by the current proxy object.
99 101 */
100 - public function __construct( ReflectionSession $session, $name )
102 + public function __construct( ParserContext $context, $name )
101 103 {
102 - $this->_setSession( $session );
104 + $this->_setContext( $context );
103 105 $this->_setName( $name );
104 106 }
105 107
106 108 /**
107 - * Sets the currently used reflection session instance.
109 + * Sets the currently used parser context instance.
108 110 *
109 - * @param \pdepend\reflection\ReflectionSession $session The currently
110 - * used reflection session instance that is used during the actual
111 + * @param \pdepend\reflection\ReflectionSession $context The currently
112 + * used parser context instance that is used during the actual
111 113 * parsing process.
112 114 *
113 115 * @return void
114 116 */
115 - private function _setSession( ReflectionSession $session )
117 + private function _setContext( ParserContext $context )
116 118 {
117 - $this->_session = $session;
119 + $this->_context = $context;
118 120 }
119 121
120 122 /**
597 599 {
598 600 if ( $this->_proxySubject === null )
599 601 {
600 - $this->_proxySubject = $this->_session->getClass( $this->_name );
602 + $this->_proxySubject = $this->_context->getClass( $this->_name );
601 603 }
602 604 return $this->_proxySubject;
603 605 }
268 269 /source/pdepend/reflection/parser/Parser.php
248 248 continue;
249 249 }
250 250
251 - $this->_context->registerClass( $class );
251 + $this->_context->addClass( $class );
252 252
253 253 $class->initStartLine( $token->startLine );
254 254 $class->initFileName( $this->_pathName );
268 269 /source/pdepend/reflection/ReflectionClassProxyContext.php
72 72 private $_session = null;
73 73
74 74 /**
75 + * A simple cache class which holds already parsed and created reflection
76 + * class instances.
75 77 *
76 - * @var array(string=>\ReflectionClass)
78 + * @var \pdepend\reflection\factories\ReflectionClassCache
77 79 */
78 - private $_classes = array();
80 + private $_classCache = null;
79 81
80 82 /**
81 83 * Constructs a new parser context instance.
87 89 public function __construct( ReflectionSession $session )
88 90 {
89 91 $this->_session = $session;
92 +
93 + $this->_classCache = new ReflectionClassCache();
90 94 }
91 95
92 96 /**
99 103 */
100 104 public function getClassReference( $className )
101 105 {
102 - if ( isset( $this->_classes[$className] ) )
106 + if ( $this->_classCache->has( $className ) )
103 107 {
104 - return $this->_classes[$className];
108 + return $this->_classCache->restore( $className );
105 109 }
106 - return new ReflectionClassProxy( $this->_session, $className );
110 + return new ReflectionClassProxy( $this, $className );
107 111 }
108 112
109 113 /**
114 + * Returns a previous registered <b>\ReflectionClass</b> instance that
115 + * matches the given class name. Or throws a reflection exception when no
116 + * matching class exists.
117 + *
118 + * @param string $className Full qualified name of the request class.
119 + *
120 + * @return \ReflectionClass
121 + * @throws \ReflectionException When no matching class or interface for the
122 + * given name exists.
123 + */
124 + public function getClass( $className )
125 + {
126 + if ( $this->_classCache->has( $className ) )
127 + {
128 + return $this->_classCache->restore( $className );
129 + }
130 + return $this->_session->getClass( $className );
131 + }
132 +
133 + /**
110 134 * This method can/should be called by the parser whenever the source of a
111 135 * class or interface has been completed.
112 136 *
115 139 *
116 140 * @return void
117 141 */
118 - public function registerClass( \ReflectionClass $class )
142 + public function addClass( \ReflectionClass $class )
119 143 {
120 - $this->_classes[$class->getName()] = $class;
144 + $this->_classCache->store( $class );
121 145 }
122 146 }