3v4l.org

run code in 300+ PHP versions simultaneously
<?php declare(strict_types=1); namespace Metadata; class ClassMetadata implements \Serializable { /** * @var string */ public $name; /** * @var MethodMetadata[] */ public $methodMetadata = []; /** * @var PropertyMetadata[] */ public $propertyMetadata = []; /** * @var string[] */ public $fileResources = []; /** * @var int */ public $createdAt; public function __construct(string $name) { $this->name = $name; $this->createdAt = time(); } public function addMethodMetadata(MethodMetadata $metadata): void { $this->methodMetadata[$metadata->name] = $metadata; } public function addPropertyMetadata(PropertyMetadata $metadata): void { $this->propertyMetadata[$metadata->name] = $metadata; } public function isFresh(?int $timestamp = null): bool { if (null === $timestamp) { $timestamp = $this->createdAt; } foreach ($this->fileResources as $filepath) { if (!file_exists($filepath)) { return false; } if ($timestamp < filemtime($filepath)) { return false; } } return true; } /** * @return string * * @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.UselessReturnAnnotation */ public function serialize() { return serialize($this->__serialize()); } /** * @param string $str * * @return void * * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint * @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.UselessReturnAnnotation */ public function unserialize($str) { $this->__unserialize(unserialize($str)); } /** * @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification */ public function __serialize(): array { return [ $this->name, $this->methodMetadata, $this->propertyMetadata, $this->fileResources, $this->createdAt, ]; } /** * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingTraversableTypeHintSpecification */ public function __unserialize(array $data): void { [ $this->name, $this->methodMetadata, $this->propertyMetadata, $this->fileResources, $this->createdAt, ] = $data; } } namespace Massive\Bundle\SearchBundle\Search\Metadata; use Metadata\ClassMetadata as BaseClassMetadata; class ClassMetadata2 extends BaseClassMetadata implements \Serializable { /** * @var array */ private $indexMetadatas = []; /** * @var string */ private $repositoryMethod; /** * Add an index metadata for the given context name. * * @param mixed $contextName */ public function addIndexMetadata($contextName, IndexMetadata $indexMetadata) { if (isset($this->indexMetadatas[$contextName])) { throw new \InvalidArgumentException(\sprintf( 'Context name "%s" has already been registered', $contextName )); } $indexMetadata->setName($this->name); $indexMetadata->setClassMetadata($this); $this->indexMetadatas[$contextName] = $indexMetadata; } /** * Return the IndexMetadata metadata instances. * * @return IndexMetadata[] */ public function getIndexMetadatas() { return $this->indexMetadatas; } /** * Return the indexmetadata for the given context. * * @param string $contextName * * @return IndexMetadata */ public function getIndexMetadata($contextName) { if (!isset($this->indexMetadatas[$contextName])) { throw new \InvalidArgumentException(\sprintf( 'Context name "%s" not known, known contexts: "%s"', $contextName, \implode('", "', \array_keys($this->indexMetadatas)) )); } return $this->indexMetadatas[$contextName]; } public function serialize() { $data = parent::serialize(); return [$data, \serialize($this->indexMetadatas), $this->repositoryMethod]; } public function unserialize($data) { list($data, $indexMetadata, $this->repositoryMethod) = \unserialize($data); parent::unserialize($data); $this->indexMetadatas = \unserialize($indexMetadata); } public function __serialize(): array { return $this->serialize(); } public function __unserialize(array $data): void { $this->unserialize($data); } /** * If specified, the reindex repsoitory method will be used to indicate a method * which can be used to modify the query builder (e.g. to exclude certain objects * from the index). * * @deprecated Returning anything from this method is deprecated. It will be passed a query builder */ public function getReindexRepositoryMethod() { return $this->repositoryMethod; } /** * Set the repository method which should be used when reindexing. * * @param string $repositoryMethod */ public function setReindexRepositoryMethod($repositoryMethod) { $this->repositoryMethod = $repositoryMethod; } } namespace App; class TestClass { } $test = new \Metadata\ClassMetadata(TestClass::class); $test2 = new \Massive\Bundle\SearchBundle\Search\Metadata\ClassMetadata2(TestClass::class); $string = serialize($test); unserialize($string); $string2 = serialize($test2); unserialize($string2);

preferences:
48.5 ms | 412 KiB | 5 Q