PHP code example of scrumworks / property-reader

1. Go to this page and download the library: Download scrumworks/property-reader library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

scrumworks / property-reader example snippets


public $var;

/**
 * @var mixed
 */
public $var;

/**
 * @var integer
 */
public int $var;

/**
 * @var int[]
 */
public array $var;

/**
 * @var SomeClass
 */
public SomeClass $var;

/**
 * @var int|string
 */
public $var;



use ScrumWorks\PropertyReader\PropertyTypeReader;
use ScrumWorks\PropertyReader\VariableTypeWriter;
use ScrumWorks\PropertyReader\VariableTypeUnifyService;

class Example
{
    public $untyped;

    public int $integer;

    /**
     * @var ?string
     */
    public $nullableString;

    /**
     * @var array<string, string[]>
     */
    public array $hashmap;

    public VariableTypeWriter $class;

    /**
     * @var int|int[]|null
     */
    public $union;
}

$reflection = new ReflectionClass(Example::class);

$variableTypeUnifyService = new VariableTypeUnifyService();
$propertyTypeReader = new PropertyTypeReader($variableTypeUnifyService);
$variableTypeWriter = new VariableTypeWriter();

foreach ($reflection->getProperties() as $propertyReflection) {
    $variableType = $propertyTypeReader->readUnifiedVariableType($propertyReflection);
    printf(
        "%s: %s\n",
        $propertyReflection->getName(),
        $variableTypeWriter->variableTypeToString($variableType)
    );
}

use ScrumWorks\PropertyReader\VariableType\ArrayVariableType;
use ScrumWorks\PropertyReader\VariableType\ScalarVariableType;

// load object...

/** @var ArrayVariableType $hashmapType */
$hashmapType = $propertyTypeReader->readUnifiedVariableType($reflection->getProperty('hashmap'));
assert($hashmapType->isNullable() === false);
assert($hashmapType->getKeyType() instanceof ScalarVariableType);
assert($hashmapType->getKeyType()->getType() === ScalarVariableType::TYPE_STRING);