Skip to content

Commit

Permalink
initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Nov 30, 2023
1 parent 43ecc76 commit d202d3b
Show file tree
Hide file tree
Showing 19 changed files with 735 additions and 27 deletions.
16 changes: 8 additions & 8 deletions .vscode/templates.code-snippets
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"body": [
"<?php",
"",
"namespace Chevere\\%namespace%;",
"namespace Chevere\\VarSupport;",
"",
"/**",
" * Describes the component in charge of ${1:doing}.",
Expand All @@ -22,7 +22,7 @@
"body": [
"<?php",
"",
"namespace Chevere\\%namespace%;",
"namespace Chevere\\VarSupport;",
"",
"final class $TM_FILENAME_BASE",
"{",
Expand All @@ -36,7 +36,7 @@
"body": [
"<?php",
"",
"namespace Chevere\\%namespace%;",
"namespace Chevere\\VarSupport;",
"",
"use Chevere\\Exceptions\\Core\\Exception;",
"",
Expand All @@ -54,7 +54,7 @@
"body": [
"<?php",
"",
"namespace Chevere\\%namespace%;",
"namespace Chevere\\VarSupport;",
"",
"trait $TM_FILENAME_BASE",
"{",
Expand All @@ -68,7 +68,7 @@
"body": [
"<?php",
"",
"namespace Chevere\\%namespace%\\\\${TM_DIRECTORY/.*src\\/(([^\\/]*)(\\/)?)|(\\/)([^\\/]*)/$2${3:+.}${5:+.}$5/g};",
"namespace Chevere\\VarSupport\\\\${TM_DIRECTORY/.*src\\/(([^\\/]*)(\\/)?)|(\\/)([^\\/]*)/$2${3:+.}${5:+.}$5/g};",
"",
"/**",
" * Describes the component in charge of ${1:doing}.",
Expand All @@ -85,7 +85,7 @@
"body": [
"<?php",
"",
"namespace Chevere\\%namespace%\\\\${TM_DIRECTORY/.*src\\/(([^\\/]*)(\\/)?)|(\\/)([^\\/]*)/$2${3:+.}${5:+.}$5/g};",
"namespace Chevere\\VarSupport\\\\${TM_DIRECTORY/.*src\\/(([^\\/]*)(\\/)?)|(\\/)([^\\/]*)/$2${3:+.}${5:+.}$5/g};",
"",
"final class $TM_FILENAME_BASE",
"{",
Expand All @@ -99,7 +99,7 @@
"body": [
"<?php",
"",
"namespace Chevere\\%namespace%\\\\${TM_DIRECTORY/.*src\\/(([^\\/]*)(\\/)?)|(\\/)([^\\/]*)/$2${3:+.}${5:+.}$5/g};",
"namespace Chevere\\VarSupport\\\\${TM_DIRECTORY/.*src\\/(([^\\/]*)(\\/)?)|(\\/)([^\\/]*)/$2${3:+.}${5:+.}$5/g};",
"",
"use Chevere\\Exceptions\\Core\\Exception;",
"",
Expand All @@ -117,7 +117,7 @@
"body": [
"<?php",
"",
"namespace Chevere\\%namespace%\\\\${TM_DIRECTORY/.*src\\/(([^\\/]*)(\\/)?)|(\\/)([^\\/]*)/$2${3:+.}${5:+.}$5/g};",
"namespace Chevere\\VarSupport\\\\${TM_DIRECTORY/.*src\\/(([^\\/]*)(\\/)?)|(\\/)([^\\/]*)/$2${3:+.}${5:+.}$5/g};",
"",
"trait $TM_FILENAME_BASE",
"{",
Expand Down
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
# %namespace%
# VarSupport

> 🔔 Subscribe to the [newsletter](https://chv.to/chevere-newsletter) to don't miss any update regarding Chevere.
![Chevere](chevere.svg)

[![Build](https://img.shields.io/github/actions/workflow/status/chevere/reponame/test.yml?branch=%branch%&style=flat-square)](https://github.com/chevere/reponame/actions)
![Code size](https://img.shields.io/github/languages/code-size/chevere/reponame?style=flat-square)
[![Apache-2.0](https://img.shields.io/github/license/chevere/reponame?style=flat-square)](LICENSE)
[![Build](https://img.shields.io/github/actions/workflow/status/chevere/var-support/test.yml?branch=1.0&style=flat-square)](https://github.com/chevere/var-support/actions)
![Code size](https://img.shields.io/github/languages/code-size/chevere/var-support?style=flat-square)
[![Apache-2.0](https://img.shields.io/github/license/chevere/var-support?style=flat-square)](LICENSE)
[![PHPStan](https://img.shields.io/badge/PHPStan-level%209-blueviolet?style=flat-square)](https://phpstan.org/)
[![Mutation testing badge](https://img.shields.io/endpoint?style=flat-square&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fchevere%2Freponame%2F%branch%)](https://dashboard.stryker-mutator.io/reports/github.com/chevere/reponame/%branch%)

[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=chevere_reponame&metric=alert_status)](https://sonarcloud.io/dashboard?id=chevere_reponame)
[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=chevere_reponame&metric=sqale_rating)](https://sonarcloud.io/dashboard?id=chevere_reponame)
[![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=chevere_reponame&metric=reliability_rating)](https://sonarcloud.io/dashboard?id=chevere_reponame)
[![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=chevere_reponame&metric=security_rating)](https://sonarcloud.io/dashboard?id=chevere_reponame)
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=chevere_reponame&metric=coverage)](https://sonarcloud.io/dashboard?id=chevere_reponame)
[![Technical Debt](https://sonarcloud.io/api/project_badges/measure?project=chevere_reponame&metric=sqale_index)](https://sonarcloud.io/dashboard?id=chevere_reponame)
[![CodeFactor](https://www.codefactor.io/repository/github/chevere/reponame/badge)](https://www.codefactor.io/repository/github/chevere/reponame)
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/b956754f8ff04aaa9ca24a6e4cc21661)](https://app.codacy.com/gh/chevere/reponame/dashboard)
[![Mutation testing badge](https://img.shields.io/endpoint?style=flat-square&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fchevere%2Fvar-support%2F1.0)](https://dashboard.stryker-mutator.io/reports/github.com/chevere/var-support/1.0)

[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=chevere_var-support&metric=alert_status)](https://sonarcloud.io/dashboard?id=chevere_var-support)
[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=chevere_var-support&metric=sqale_rating)](https://sonarcloud.io/dashboard?id=chevere_var-support)
[![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=chevere_var-support&metric=reliability_rating)](https://sonarcloud.io/dashboard?id=chevere_var-support)
[![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=chevere_var-support&metric=security_rating)](https://sonarcloud.io/dashboard?id=chevere_var-support)
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=chevere_var-support&metric=coverage)](https://sonarcloud.io/dashboard?id=chevere_var-support)
[![Technical Debt](https://sonarcloud.io/api/project_badges/measure?project=chevere_var-support&metric=sqale_index)](https://sonarcloud.io/dashboard?id=chevere_var-support)
[![CodeFactor](https://www.codefactor.io/repository/github/chevere/var-support/badge)](https://www.codefactor.io/repository/github/chevere/var-support)
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/b956754f8ff04aaa9ca24a6e4cc21661)](https://app.codacy.com/gh/chevere/var-support/dashboard)

## Documentation

Expand Down
11 changes: 7 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chevere/reponame",
"description": "A chevere reponame package",
"name": "chevere/var-support",
"description": "A chevere var-support package",
"homepage": "https://chevere.org",
"type": "library",
"license": "Apache-2.0",
Expand All @@ -12,7 +12,10 @@
}
],
"require": {
"chevere/chevere": "^3.0"
"php": "^8.1",
"chevere/iterator": "^1.0",
"chevere/message": "^0.1.0",
"symfony/var-exporter": "^7.0"
},
"require-dev": {
"phpstan/phpstan": "^1.9",
Expand All @@ -24,7 +27,7 @@
"src/functions.php"
],
"psr-4": {
"Chevere\\%namespace%\\": "src/"
"Chevere\\VarSupport\\": "src/"
}
},
"autoload-dev": {
Expand Down
2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
sonar.projectKey=chevere_reponame
sonar.projectKey=chevere_var-support
sonar.organization=chevere
sonar.host.url=https://sonarcloud.io
sonar.sourceEncoding=UTF-8
Expand Down
Empty file removed src/.git-keep
Empty file.
23 changes: 23 additions & 0 deletions src/Exceptions/ObjectNotClonableException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of Chevere.
*
* (c) Rodolfo Berrios <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Chevere\VarSupport\Exceptions;

use LogicException;

/**
* Exception thrown when failing to provide a clonable object.
*/
final class ObjectNotClonableException extends LogicException
{
}
23 changes: 23 additions & 0 deletions src/Exceptions/UnableToStoreException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of Chevere.
*
* (c) Rodolfo Berrios <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Chevere\VarSupport\Exceptions;

use LogicException;

/**
* Exception thrown when a variable can't be stored.
*/
final class UnableToStoreException extends LogicException
{
}
24 changes: 24 additions & 0 deletions src/Interfaces/ObjectVariableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of Chevere.
*
* (c) Rodolfo Berrios <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Chevere\VarSupport\Interfaces;

/**
* Describes the component in charge of defining an object variable.
*/
interface ObjectVariableInterface
{
public function variable(): object;

public function assertClonable(): void;
}
37 changes: 37 additions & 0 deletions src/Interfaces/StorableVariableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of Chevere.
*
* (c) Rodolfo Berrios <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Chevere\VarSupport\Interfaces;

/**
* Describes the component in charge of handling storable variable.
*/
interface StorableVariableInterface
{
public function __construct(mixed $variable);

/**
* Provides access to passed `$variable`.
*/
public function variable(): mixed;

/**
* Shorthand for `\var_export($variable)`.
*/
public function toExport(): string;

/**
* Shorthand for `\serialize($variable)`.
*/
public function toSerialize(): string;
}
97 changes: 97 additions & 0 deletions src/ObjectVariable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

/*
* This file is part of Chevere.
*
* (c) Rodolfo Berrios <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Chevere\VarSupport;

use Chevere\Iterator\Breadcrumb;
use Chevere\Iterator\Interfaces\BreadcrumbInterface;
use Chevere\VarSupport\Exceptions\ObjectNotClonableException;
use Chevere\VarSupport\Interfaces\ObjectVariableInterface;
use Chevere\VarSupport\Traits\BreadcrumbIterableTrait;
use ReflectionNamedType;
use ReflectionObject;
use function Chevere\Message\message;

final class ObjectVariable implements ObjectVariableInterface
{
use BreadcrumbIterableTrait;

private BreadcrumbInterface $breadcrumb;

public function __construct(
private object $variable
) {
$this->breadcrumb = new Breadcrumb();
}

public function variable(): object
{
return $this->variable;
}

/**
* @throws ObjectNotClonableException
*/
public function assertClonable(): void
{
$this->assert($this->variable);
}

private function assert(mixed $variable): void
{
if (is_object($variable)) {
$this->breadcrumbObject($variable);
} elseif (is_iterable($variable)) {
$this->breadcrumbIterable($variable);
}
}

private function breadcrumbObject(object $variable): void
{
$this->breadcrumb = $this->breadcrumb
->withAdded('object: ' . $variable::class);
$objectKey = $this->breadcrumb->pos();
$reflection = new ReflectionObject($variable);
if (! $reflection->isCloneable()) {
throw new ObjectNotClonableException(
(string) message(
'Object is not clonable at `%at%`',
at: $this->breadcrumb->__toString()
)
);
}
$properties = $reflection->getProperties();
foreach ($properties as $property) {
/** @var ?ReflectionNamedType $namedType */
$namedType = $property->getType();
$propertyType = $namedType !== null
? $namedType->getName() . ' '
: '';
$this->breadcrumb = $this->breadcrumb
->withAdded(
'property: '
. $propertyType
. '$' . $property->getName()
);
$propertyKey = $this->breadcrumb->pos();
// @infection-ignore-all
if ($property->isInitialized($variable)) {
$this->assert($property->getValue($variable));
}
$this->breadcrumb = $this->breadcrumb
->withRemoved($propertyKey);
}
$this->breadcrumb = $this->breadcrumb
->withRemoved($objectKey);
}
}
Loading

0 comments on commit d202d3b

Please sign in to comment.