@@ -578,15 +578,28 @@ private function userDefinedInterfaceMethods(string $interfaceName): array
578
578
private function getObject (MockType $ mockClass , string $ type = '' , bool $ callOriginalConstructor = false , array $ arguments = [], bool $ callOriginalMethods = false , ?object $ proxyTarget = null , bool $ returnValueGeneration = true ): object
579
579
{
580
580
$ className = $ mockClass ->generate ();
581
- $ object = $ this ->instantiate ($ className , $ callOriginalConstructor , $ arguments );
581
+
582
+ try {
583
+ $ object = (new ReflectionClass ($ className ))->newInstanceWithoutConstructor ();
584
+ // @codeCoverageIgnoreStart
585
+ } catch (\ReflectionException $ e ) {
586
+ throw new ReflectionException (
587
+ $ e ->getMessage (),
588
+ $ e ->getCode (),
589
+ $ e ,
590
+ );
591
+ // @codeCoverageIgnoreEnd
592
+ }
593
+
594
+ $ reflector = new ReflectionObject ($ object );
582
595
583
596
if ($ object instanceof StubInternal && $ mockClass instanceof MockClass) {
584
597
/**
585
598
* @psalm-suppress MissingThrowsDocblock
586
599
*
587
600
* @noinspection PhpUnhandledExceptionInspection
588
601
*/
589
- ( new ReflectionObject ( $ object )) ->getProperty ('__phpunit_state ' )->setValue (
602
+ $ reflector ->getProperty ('__phpunit_state ' )->setValue (
590
603
$ object ,
591
604
new TestDoubleState ($ mockClass ->configurableMethods (), $ returnValueGeneration ),
592
605
);
@@ -596,6 +609,20 @@ private function getObject(MockType $mockClass, string $type = '', bool $callOri
596
609
}
597
610
}
598
611
612
+ if ($ callOriginalConstructor && $ reflector ->getConstructor () !== null ) {
613
+ try {
614
+ $ reflector ->getConstructor ()->invokeArgs ($ object , $ arguments );
615
+ // @codeCoverageIgnoreStart
616
+ } catch (\ReflectionException $ e ) {
617
+ throw new ReflectionException (
618
+ $ e ->getMessage (),
619
+ $ e ->getCode (),
620
+ $ e ,
621
+ );
622
+ // @codeCoverageIgnoreEnd
623
+ }
624
+ }
625
+
599
626
return $ object ;
600
627
}
601
628
@@ -1003,44 +1030,6 @@ trait_exists($className, false)) {
1003
1030
}
1004
1031
}
1005
1032
1006
- /**
1007
- * @psalm-param class-string $className
1008
- *
1009
- * @throws ReflectionException
1010
- */
1011
- private function instantiate (string $ className , bool $ callOriginalConstructor , array $ arguments ): object
1012
- {
1013
- if ($ callOriginalConstructor ) {
1014
- if (count ($ arguments ) === 0 ) {
1015
- return new $ className ;
1016
- }
1017
-
1018
- try {
1019
- return (new ReflectionClass ($ className ))->newInstanceArgs ($ arguments );
1020
- // @codeCoverageIgnoreStart
1021
- } catch (\ReflectionException $ e ) {
1022
- throw new ReflectionException (
1023
- $ e ->getMessage (),
1024
- $ e ->getCode (),
1025
- $ e ,
1026
- );
1027
- }
1028
- // @codeCoverageIgnoreEnd
1029
- }
1030
-
1031
- try {
1032
- return (new ReflectionClass ($ className ))->newInstanceWithoutConstructor ();
1033
- // @codeCoverageIgnoreStart
1034
- } catch (\ReflectionException $ e ) {
1035
- throw new ReflectionException (
1036
- $ e ->getMessage (),
1037
- $ e ->getCode (),
1038
- $ e ,
1039
- );
1040
- // @codeCoverageIgnoreEnd
1041
- }
1042
- }
1043
-
1044
1033
/**
1045
1034
* @psalm-param class-string $type
1046
1035
*
0 commit comments