/** * is $item something that can be used in a foreach() loop? * * @param mixed $item * the item to examine * @return boolean * true if the item can be used in a foreach() loop * false otherwise */ private static function calculateResult($item) { $itemTypes = AllMatchingTypesList::from($item); foreach ($itemTypes as $itemType) { if (isset(self::$acceptableTypes[$itemType])) { return true; } } return false; }
/** * @covers ::fromString */ public function testDetectsCallableStrings() { // ---------------------------------------------------------------- // setup your test $data = 'is_string'; $expectedResult = ['Callable', 'String', 'EverythingElse']; // ---------------------------------------------------------------- // perform the change $actualResult = AllMatchingTypesList::from($data); // ---------------------------------------------------------------- // test the results $this->assertEquals($expectedResult, $actualResult); }
/** * use an input item's data type to work out which method we should * call * * this is a faster replacement for the older FirstMethodMatchingType * value builder * * not only is it faster, but the lookup table can also contain protected * and private methods * * @param mixed $item * the item we want to dispatch * @param array $typeMethods * the list of methods that are available * @return string * the name of the method to call */ public static function using($item, $typeMethods) { $types = AllMatchingTypesList::from($item); foreach ($types as $type) { if (isset($typeMethods[$type])) { return $typeMethods[$type]; } } return 'nothingMatchesTheInputType'; }
/** * get a list of which subclasses a VersionNumber uses * * our requirement (for this check) is to ensure that programmers can * subclass version numbers, and their subclasses still pass this check * * for example, given this: * * class MyVersion extends SemanticVersion { ... } * * objects of type MyVersion should be compatible with: * * - other objects of type MyVersion * - any objects of type SemanticVersion * * but they cannot be compatible with objects of type VersionNumber * * @param VersionNumber $versionNumber * the object to study * @return array * a list of the subclasses that $versionNumber will be * compatible with */ private static function getCompatibleVersionSubclasses(VersionNumber $versionNumber) { $retval = []; $list = AllMatchingTypesList::from($versionNumber); foreach ($list as $type) { if ($type === VersionNumber::class) { return $retval; } $retval[$type] = $type; } // @codeCoverageIgnoreStart UnreachableCode::alert(); }
/** * find the first method on $target that matches $data's data type * * @param mixed $data * the data we want to call $target with * @param string|object $target * the target that we want to call * @param string $methodPrefix * the prefix at the front of methods on $target * @return string|null * the method that suits $data the best */ private static function findFirstMatchingMethod($data, $target, $methodPrefix) { // no, so we need to build it $possibleTypes = AllMatchingTypesList::from($data); $possibleMethods = CallableMethodsList::from($target); foreach ($possibleTypes as $possibleType) { $targetMethodName = $methodPrefix . FilterNamespace::fromString($possibleType); if (isset($possibleMethods[$targetMethodName])) { // all done return $targetMethodName; } } // no match return null; }
/** * is $data compatible with $constraint? * * @param string $data * the class name to check * @param string|object $constraint * the class or object that $data must be compatible with * @return boolean * TRUE if $data is compatible * FALSE otherwise */ public static function checkString($data, $constraint) { // defensive programming! RequireStringy::check($data, E4xx_UnsupportedType::class); RequireAnyOneOf::check([new IsObject(), new IsStringy()], [$constraint], E4xx_UnsupportedType::class); $compatibleTypes = AllMatchingTypesList::from($data); if (is_object($constraint)) { $constraint = get_class($constraint); } // is our constraint in the list of data types that $data can be? if (in_array($constraint, $compatibleTypes)) { return true; } // if we get here, we have run out of ideas return false; }