GetArrayTypes::getArrayTypes()

Not yet in a tagged release

Description

GetArrayTypes::getArrayTypes() returns a list of all strict PHP types for a given PHP array. The list is ordered with the most specific match first.

use GanbaroDigital\MissingBits\TypeInspectors\GetArrayTypes;
public array GetArrayTypes::getArrayTypes(mixed $item);

Parameters

The input parameters are:

Return Value

GetArrayTypes::getArrayTypes() returns an array.

The resulting list is a complete list of strict types where it is safe to use $item.

Although PHP arrays and Traversable objects are safe to use in a foreach() loop, PHP 7.0 type declarations won't let you pass an array into a function or method that expects a Traversable.

That's why GetArrayTypes::getArrayTypes() does not include Traversable in the returned list.

You can't pass an ArrayObject, ArrayAccess or a Traversable into any of the array_XXX() functions.

That's why GetArrayTypes::getArrayTypes() returns an empty list for ArrayObject, ArrayAccess, and Traversable.

Examples

Here's a list of examples of accepted input values:

$inspector = new GetArrayTypes;
var_dump($inspector->getArrayTypes([1,2,3]));

// outputs
//
// array(1) {
//   ["array"]=>
//   string(5) "array"
// }
use GanbaroDigital\MissingBits\TypeInspectors\GetStrictTypes;

$inspector = new GetArrayTypes;
var_dump($inspector->getArrayTypes([GetStrictTypes::class, "from"]));

// outputs
//
// array(2) {
//   ["callable"]=>
//   string(8) "callable"
//   ["array"]=>
//   string(5) "array"
// }

Here's a list of examples of ingored input values:

$inspector = new GetArrayTypes;
var_dump($inspector->getArrayTypes(null));

// outputs
//
// array(0) {
// }
$inspector = new GetArrayTypes;
var_dump($inspector->getArrayTypes(function(){}));

// outputs
//
// array(0) {
// }
$inspector = new GetArrayTypes;
var_dump($inspector->getArrayTypes(true));

// outputs
//
// array(0) {
// }
$inspector = new GetArrayTypes;
var_dump($inspector->getArrayTypes(false));

// outputs
//
// array(0) {
// }
$inspector = new GetArrayTypes;
var_dump($inspector->getArrayTypes(0.0));

// outputs
//
// array(0) {
// }
$inspector = new GetArrayTypes;
var_dump($inspector->getArrayTypes(3.1415927));

// outputs
//
// array(0) {
// }
$inspector = new GetArrayTypes;
var_dump($inspector->getArrayTypes(0));

// outputs
//
// array(0) {
// }
$inspector = new GetArrayTypes;
var_dump($inspector->getArrayTypes(100));

// outputs
//
// array(0) {
// }
$inspector = new GetArrayTypes;
var_dump($inspector->getArrayTypes(-100));

// outputs
//
// array(0) {
// }
$inspector = new GetArrayTypes;
var_dump($inspector->getArrayTypes(new ArrayObject));

// outputs
//
// array(0) {
// }
use GanbaroDigital\MissingBits\TypeInspectors\GetStrictTypes;

$inspector = new GetArrayTypes;
var_dump($inspector->getArrayTypes(new GetStrictTypes));

// outputs
//
// array(0) {
// }
$inspector = new GetArrayTypes;
var_dump($inspector->getArrayTypes((object)[]));

// outputs
//
// array(0) {
// }
$inspector = new GetArrayTypes;
var_dump($inspector->getArrayTypes(STDIN));

// outputs
//
// array(0) {
// }
$inspector = new GetArrayTypes;
var_dump($inspector->getArrayTypes("true"));

// outputs
//
// array(0) {
// }
$inspector = new GetArrayTypes;
var_dump($inspector->getArrayTypes("false"));

// outputs
//
// array(0) {
// }
$inspector = new GetArrayTypes;
var_dump($inspector->getArrayTypes("0.0"));

// outputs
//
// array(0) {
// }
$inspector = new GetArrayTypes;
var_dump($inspector->getArrayTypes("3.1415927"));

// outputs
//
// array(0) {
// }
$inspector = new GetArrayTypes;
var_dump($inspector->getArrayTypes("0"));

// outputs
//
// array(0) {
// }
$inspector = new GetArrayTypes;
var_dump($inspector->getArrayTypes("100"));

// outputs
//
// array(0) {
// }
$inspector = new GetArrayTypes;
var_dump($inspector->getArrayTypes(new Exception(__FILE__)));

// outputs
//
// array(0) {
// }
$inspector = new GetArrayTypes;
var_dump($inspector->getArrayTypes("hello, world!"));

// outputs
//
// array(0) {
// }
$inspector = new GetArrayTypes;
var_dump($inspector->getArrayTypes(ArrayObject::class));

// outputs
//
// array(0) {
// }
$inspector = new GetArrayTypes;
var_dump($inspector->getArrayTypes(Traversable::class));

// outputs
//
// array(0) {
// }

Throws

GetArrayTypes::getArrayTypes() does not throw any exceptions.

Works With

GetArrayTypes::getArrayTypes() is supported on these versions of PHP:

PHP Version Works?
5.5 Yes
5.6 Yes
7.0 Yes
HHVM Yes