From 322aee0882608c853cae88a1ebdd5a67690827e5 Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Sat, 16 Dec 2017 10:57:42 +0100 Subject: [PATCH] Fix CS/WS issues --- src/Builder/Identity.php | 2 +- src/Builder/InvocationMocker.php | 16 +- src/Builder/Match.php | 4 +- src/Generator.php | 223 +++++++++++++------------- src/Invocation/Invocation.php | 2 +- src/Invocation/StaticInvocation.php | 4 +- src/InvocationMocker.php | 10 +- src/Matcher.php | 16 +- src/Matcher/ConsecutiveParameters.php | 4 +- src/Matcher/MethodName.php | 1 + src/Matcher/Parameters.php | 12 +- src/MockBuilder.php | 4 +- src/MockObject.php | 20 +-- 13 files changed, 161 insertions(+), 157 deletions(-) diff --git a/src/Builder/Identity.php b/src/Builder/Identity.php index 52629255..bc1cd1bf 100644 --- a/src/Builder/Identity.php +++ b/src/Builder/Identity.php @@ -24,7 +24,7 @@ interface Identity * * @note The identifier is unique per mock object. * - * @param string $id Unique identification of expectation. + * @param string $id unique identification of expectation */ public function id($id); } diff --git a/src/Builder/InvocationMocker.php b/src/Builder/InvocationMocker.php index b799422c..7536179c 100644 --- a/src/Builder/InvocationMocker.php +++ b/src/Builder/InvocationMocker.php @@ -204,9 +204,9 @@ public function after($id) /** * @param array ...$arguments * - * @return InvocationMocker - * * @throws RuntimeException + * + * @return InvocationMocker */ public function with(...$arguments) { @@ -220,9 +220,9 @@ public function with(...$arguments) /** * @param array ...$arguments * - * @return InvocationMocker - * * @throws RuntimeException + * + * @return InvocationMocker */ public function withConsecutive(...$arguments) { @@ -234,9 +234,9 @@ public function withConsecutive(...$arguments) } /** - * @return InvocationMocker - * * @throws RuntimeException + * + * @return InvocationMocker */ public function withAnyParameters() { @@ -250,9 +250,9 @@ public function withAnyParameters() /** * @param Constraint|string $constraint * - * @return InvocationMocker - * * @throws RuntimeException + * + * @return InvocationMocker */ public function method($constraint) { diff --git a/src/Builder/Match.php b/src/Builder/Match.php index 19ecf088..42787f23 100644 --- a/src/Builder/Match.php +++ b/src/Builder/Match.php @@ -17,8 +17,8 @@ interface Match extends Stub /** * Defines the expectation which must occur before the current is valid. * - * @param string $id The identification of the expectation that should - * occur before this one. + * @param string $id the identification of the expectation that should + * occur before this one * * @return Stub */ diff --git a/src/Generator.php b/src/Generator.php index 7c28df7a..7b52274c 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -68,12 +68,12 @@ class Generator * @param object $proxyTarget * @param bool $allowMockingUnknownTypes * - * @return MockObject - * * @throws Exception * @throws RuntimeException * @throws \PHPUnit\Framework\Exception * @throws \ReflectionException + * + * @return MockObject */ public function getMock($type, $methods = [], array $arguments = [], $mockClassName = '', $callOriginalConstructor = true, $callOriginalClone = true, $callAutoload = true, $cloneArguments = true, $callOriginalMethods = false, $proxyTarget = null, $allowMockingUnknownTypes = true) { @@ -201,70 +201,6 @@ function ($type) { ); } - /** - * @param string $code - * @param string $className - * @param array|string $type - * @param bool $callOriginalConstructor - * @param bool $callAutoload - * @param array $arguments - * @param bool $callOriginalMethods - * @param object $proxyTarget - * - * @return MockObject - * - * @throws \ReflectionException - * @throws RuntimeException - */ - private function getObject($code, $className, $type = '', $callOriginalConstructor = false, $callAutoload = false, array $arguments = [], $callOriginalMethods = false, $proxyTarget = null) - { - $this->evalClass($code, $className); - - if ($callOriginalConstructor && - \is_string($type) && - !\interface_exists($type, $callAutoload)) { - if (\count($arguments) === 0) { - $object = new $className; - } else { - $class = new ReflectionClass($className); - $object = $class->newInstanceArgs($arguments); - } - } else { - try { - $instantiator = new Instantiator; - $object = $instantiator->instantiate($className); - } catch (InstantiatorException $exception) { - throw new RuntimeException($exception->getMessage()); - } - } - - if ($callOriginalMethods) { - if (!\is_object($proxyTarget)) { - if (\count($arguments) === 0) { - $proxyTarget = new $type; - } else { - $class = new ReflectionClass($type); - $proxyTarget = $class->newInstanceArgs($arguments); - } - } - - $object->__phpunit_setOriginalObject($proxyTarget); - } - - return $object; - } - - /** - * @param string $code - * @param string $className - */ - private function evalClass($code, $className) - { - if (!\class_exists($className, false)) { - eval($code); - } - } - /** * Returns a mock object for the specified abstract class with all abstract * methods of the class mocked. Concrete methods to mock can be specified with @@ -279,11 +215,11 @@ private function evalClass($code, $className) * @param array $mockedMethods * @param bool $cloneArguments * - * @return MockObject - * * @throws \ReflectionException * @throws RuntimeException * @throws Exception + * + * @return MockObject */ public function getMockForAbstractClass($originalClassName, array $arguments = [], $mockClassName = '', $callOriginalConstructor = true, $callOriginalClone = true, $callAutoload = true, $mockedMethods = [], $cloneArguments = true) { @@ -341,11 +277,11 @@ public function getMockForAbstractClass($originalClassName, array $arguments = [ * @param array $mockedMethods * @param bool $cloneArguments * - * @return MockObject - * * @throws \ReflectionException * @throws RuntimeException * @throws Exception + * + * @return MockObject */ public function getMockForTrait($traitName, array $arguments = [], $mockClassName = '', $callOriginalConstructor = true, $callOriginalClone = true, $callAutoload = true, $mockedMethods = [], $cloneArguments = true) { @@ -400,11 +336,11 @@ public function getMockForTrait($traitName, array $arguments = [], $mockClassNam * @param bool $callOriginalClone * @param bool $callAutoload * - * @return object - * * @throws \ReflectionException * @throws RuntimeException * @throws Exception + * + * @return object */ public function getObjectForTrait($traitName, array $arguments = [], $traitClassName = '', $callOriginalConstructor = true, $callOriginalClone = true, $callAutoload = true) { @@ -453,10 +389,10 @@ public function getObjectForTrait($traitName, array $arguments = [], $traitClass * @param bool $cloneArguments * @param bool $callOriginalMethods * - * @return array - * * @throws \ReflectionException * @throws \PHPUnit\Framework\MockObject\RuntimeException + * + * @return array */ public function generate($type, array $methods = null, $mockClassName = '', $callOriginalClone = true, $callAutoload = true, $cloneArguments = true, $callOriginalMethods = false) { @@ -501,9 +437,9 @@ public function generate($type, array $methods = null, $mockClassName = '', $cal * @param array $methods * @param array $options * - * @return string - * * @throws RuntimeException + * + * @return string */ public function generateClassFromWsdl($wsdlFile, $className, array $methods = [], array $options = []) { @@ -584,19 +520,104 @@ public function generateClassFromWsdl($wsdlFile, $className, array $methods = [] } /** + * @param string $className + * + * @throws \ReflectionException + * + * @return array + */ + public function getClassMethods($className) + { + $class = new ReflectionClass($className); + $methods = []; + + foreach ($class->getMethods() as $method) { + if ($method->isPublic() || $method->isAbstract()) { + $methods[] = $method->getName(); + } + } + + return $methods; + } + + /** + * @param string $code + * @param string $className * @param array|string $type - * @param array|null $methods + * @param bool $callOriginalConstructor + * @param bool $callAutoload + * @param array $arguments + * @param bool $callOriginalMethods + * @param object $proxyTarget + * + * @throws \ReflectionException + * @throws RuntimeException + * + * @return MockObject + */ + private function getObject($code, $className, $type = '', $callOriginalConstructor = false, $callAutoload = false, array $arguments = [], $callOriginalMethods = false, $proxyTarget = null) + { + $this->evalClass($code, $className); + + if ($callOriginalConstructor && + \is_string($type) && + !\interface_exists($type, $callAutoload)) { + if (\count($arguments) === 0) { + $object = new $className; + } else { + $class = new ReflectionClass($className); + $object = $class->newInstanceArgs($arguments); + } + } else { + try { + $instantiator = new Instantiator; + $object = $instantiator->instantiate($className); + } catch (InstantiatorException $exception) { + throw new RuntimeException($exception->getMessage()); + } + } + + if ($callOriginalMethods) { + if (!\is_object($proxyTarget)) { + if (\count($arguments) === 0) { + $proxyTarget = new $type; + } else { + $class = new ReflectionClass($type); + $proxyTarget = $class->newInstanceArgs($arguments); + } + } + + $object->__phpunit_setOriginalObject($proxyTarget); + } + + return $object; + } + + /** + * @param string $code + * @param string $className + */ + private function evalClass($code, $className) + { + if (!\class_exists($className, false)) { + eval($code); + } + } + + /** + * @param array|string $type + * @param null|array $methods * @param string $mockClassName * @param bool $callOriginalClone * @param bool $callAutoload * @param bool $cloneArguments * @param bool $callOriginalMethods * - * @return array - * * @throws \InvalidArgumentException * @throws \ReflectionException * @throws RuntimeException + * + * @return array */ private function generateMock($type, $methods, $mockClassName, $callOriginalClone, $callAutoload, $cloneArguments, $callOriginalMethods) { @@ -623,7 +644,8 @@ private function generateMock($type, $methods, $mockClassName, $callOriginalClon $isMultipleInterfaces = true; $additionalInterfaces[] = $_type; - $typeClass = new ReflectionClass($this->generateClassName( + $typeClass = new ReflectionClass( + $this->generateClassName( $_type, $mockClassName, 'Mock_' @@ -891,9 +913,9 @@ private function generateMockClassDeclaration(array $mockClassName, $isInterface * @param bool $cloneArguments * @param bool $callOriginalMethods * - * @return string - * * @throws \PHPUnit\Framework\MockObject\RuntimeException + * + * @return string */ private function generateMockedMethodDefinitionFromExisting(ReflectionMethod $method, $cloneArguments, $callOriginalMethods) { @@ -957,9 +979,9 @@ private function generateMockedMethodDefinitionFromExisting(ReflectionMethod $me * @param bool|string $deprecation * @param bool $allowsReturnNull * - * @return string - * * @throws \InvalidArgumentException + * + * @return string */ private function generateMockedMethodDefinition($className, $methodName, $cloneArguments = true, $modifier = 'public', $argumentsForDeclaration = '', $argumentsForCall = '', $returnType = '', $reference = '', $callOriginalMethods = false, $static = false, $deprecation = false, $allowsReturnNull = false) { @@ -1023,9 +1045,9 @@ private function generateMockedMethodDefinition($className, $methodName, $cloneA /** * @param ReflectionMethod $method * - * @return bool - * * @throws \ReflectionException + * + * @return bool */ private function canMockMethod(ReflectionMethod $method) { @@ -1050,9 +1072,9 @@ private function isMethodNameBlacklisted($name) * @param ReflectionMethod $method * @param bool $forCall * - * @return string - * * @throws RuntimeException + * + * @return string */ private function getMethodParameters(ReflectionMethod $method, $forCall = false) { @@ -1133,33 +1155,12 @@ private function getMethodParameters(ReflectionMethod $method, $forCall = false) return \implode(', ', $parameters); } - /** - * @param string $className - * - * @return array - * - * @throws \ReflectionException - */ - public function getClassMethods($className) - { - $class = new ReflectionClass($className); - $methods = []; - - foreach ($class->getMethods() as $method) { - if ($method->isPublic() || $method->isAbstract()) { - $methods[] = $method->getName(); - } - } - - return $methods; - } - /** * @param string $template * - * @return Text_Template - * * @throws \InvalidArgumentException + * + * @return Text_Template */ private function getTemplate($template) { diff --git a/src/Invocation/Invocation.php b/src/Invocation/Invocation.php index d82c1505..e10bf2a8 100644 --- a/src/Invocation/Invocation.php +++ b/src/Invocation/Invocation.php @@ -15,7 +15,7 @@ interface Invocation { /** - * @return mixed Mocked return value. + * @return mixed mocked return value */ public function generateReturnValue(); diff --git a/src/Invocation/StaticInvocation.php b/src/Invocation/StaticInvocation.php index a9254589..170bcbba 100644 --- a/src/Invocation/StaticInvocation.php +++ b/src/Invocation/StaticInvocation.php @@ -127,11 +127,11 @@ public function isReturnTypeNullable(): bool } /** - * @return mixed Mocked return value - * * @throws \ReflectionException * @throws \PHPUnit\Framework\MockObject\RuntimeException * @throws \PHPUnit\Framework\Exception + * + * @return mixed Mocked return value */ public function generateReturnValue() { diff --git a/src/InvocationMocker.php b/src/InvocationMocker.php index f3fbece7..d57dbcf4 100644 --- a/src/InvocationMocker.php +++ b/src/InvocationMocker.php @@ -70,7 +70,7 @@ public function hasMatchers() /** * @param mixed $id * - * @return bool|null + * @return null|bool */ public function lookupId($id) { @@ -115,9 +115,9 @@ public function expects(MatcherInvocation $matcher) /** * @param Invocation $invocation * - * @return mixed - * * @throws Exception + * + * @return mixed */ public function invoke(Invocation $invocation) { @@ -172,9 +172,9 @@ public function matches(Invocation $invocation) } /** - * @return bool - * * @throws \PHPUnit\Framework\ExpectationFailedException + * + * @return bool */ public function verify() { diff --git a/src/Matcher.php b/src/Matcher.php index 680a844e..505e26d2 100644 --- a/src/Matcher.php +++ b/src/Matcher.php @@ -37,7 +37,7 @@ class Matcher implements MatcherInvocation /** * @var mixed */ - private $afterMatchBuilderId = null; + private $afterMatchBuilderId; /** * @var bool @@ -47,17 +47,17 @@ class Matcher implements MatcherInvocation /** * @var MethodName */ - private $methodNameMatcher = null; + private $methodNameMatcher; /** * @var Parameters */ - private $parametersMatcher = null; + private $parametersMatcher; /** * @var Stub */ - private $stub = null; + private $stub; /** * @param MatcherInvocation $invocationMatcher @@ -115,11 +115,11 @@ public function setAfterMatchBuilderId($id) /** * @param Invocation $invocation * - * @return mixed - * * @throws \Exception * @throws RuntimeException * @throws ExpectationFailedException + * + * @return mixed */ public function invoked(Invocation $invocation) { @@ -183,10 +183,10 @@ public function invoked(Invocation $invocation) /** * @param Invocation $invocation * - * @return bool - * * @throws RuntimeException * @throws ExpectationFailedException + * + * @return bool */ public function matches(Invocation $invocation) { diff --git a/src/Matcher/ConsecutiveParameters.php b/src/Matcher/ConsecutiveParameters.php index 432e2e45..216dd81a 100644 --- a/src/Matcher/ConsecutiveParameters.php +++ b/src/Matcher/ConsecutiveParameters.php @@ -65,9 +65,9 @@ public function toString(): string /** * @param BaseInvocation $invocation * - * @return bool - * * @throws \PHPUnit\Framework\ExpectationFailedException + * + * @return bool */ public function matches(BaseInvocation $invocation) { diff --git a/src/Matcher/MethodName.php b/src/Matcher/MethodName.php index 06f969c6..b065a847 100644 --- a/src/Matcher/MethodName.php +++ b/src/Matcher/MethodName.php @@ -30,6 +30,7 @@ class MethodName extends StatelessInvocation /** * @param Constraint|string + * @param mixed $constraint * * @throws Constraint * @throws \PHPUnit\Framework\Exception diff --git a/src/Matcher/Parameters.php b/src/Matcher/Parameters.php index 39d765d6..7519d4e5 100644 --- a/src/Matcher/Parameters.php +++ b/src/Matcher/Parameters.php @@ -78,9 +78,9 @@ public function toString(): string /** * @param BaseInvocation $invocation * - * @return bool - * * @throws \Exception + * + * @return bool */ public function matches(BaseInvocation $invocation) { @@ -103,9 +103,9 @@ public function matches(BaseInvocation $invocation) * does the matcher will get the invoked() method called which should check * if an expectation is met. * - * @return bool - * * @throws ExpectationFailedException + * + * @return bool */ public function verify() { @@ -150,9 +150,9 @@ public function verify() } /** - * @return bool - * * @throws ExpectationFailedException + * + * @return bool */ private function guardAgainstDuplicateEvaluationOfParameterConstraints() { diff --git a/src/MockBuilder.php b/src/MockBuilder.php index 3dcab7f9..5481c0ef 100644 --- a/src/MockBuilder.php +++ b/src/MockBuilder.php @@ -74,7 +74,7 @@ class MockBuilder /** * @var object */ - private $proxyTarget = null; + private $proxyTarget; /** * @var bool @@ -172,7 +172,7 @@ public function getMockForTrait() /** * Specifies the subset of methods to mock. Default is to mock none of them. * - * @param array|null $methods + * @param null|array $methods * * @return MockBuilder */ diff --git a/src/MockObject.php b/src/MockObject.php index ead504f4..585b207c 100644 --- a/src/MockObject.php +++ b/src/MockObject.php @@ -21,16 +21,8 @@ interface PHPUnit_Framework_MockObject_MockObject /*extends Verifiable*/ { /** - * Registers a new expectation in the mock object and returns the match - * object which can be infused with further details. + * @param mixed $originalObject * - * @param Invocation $matcher - * - * @return InvocationMocker - */ - public function expects(Invocation $matcher); - - /** * @return InvocationMocker */ public function __phpunit_setOriginalObject($originalObject); @@ -52,4 +44,14 @@ public function __phpunit_verify(); * @return bool */ public function __phpunit_hasMatchers(); + + /** + * Registers a new expectation in the mock object and returns the match + * object which can be infused with further details. + * + * @param Invocation $matcher + * + * @return InvocationMocker + */ + public function expects(Invocation $matcher); }