Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Mar 12, 2017
0 parents commit 2201553
Show file tree
Hide file tree
Showing 15 changed files with 409 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
/.idea
/.php_cs.cache
/composer.lock
/vendor
79 changes: 79 additions & 0 deletions .php_cs
@@ -0,0 +1,79 @@
<?php
$header = <<<'EOF'
This file is part of object-reflector.
(c) Sebastian Bergmann <sebastian@phpunit.de>
For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.
EOF;

return PhpCsFixer\Config::create()
->setRiskyAllowed(true)
->setRules(
[
'array_syntax' => ['syntax' => 'short'],
'binary_operator_spaces' => [
'align_double_arrow' => true,
'align_equals' => true
],
'blank_line_after_namespace' => true,
'blank_line_before_return' => true,
'braces' => true,
'cast_spaces' => true,
'concat_space' => ['spacing' => 'one'],
'declare_strict_types' => true,
'elseif' => true,
'encoding' => true,
'full_opening_tag' => true,
'function_declaration' => true,
#'header_comment' => ['header' => $header, 'separate' => 'none'],
'indentation_type' => true,
'line_ending' => true,
'lowercase_constants' => true,
'lowercase_keywords' => true,
'method_argument_space' => true,
'no_alias_functions' => true,
'no_blank_lines_after_class_opening' => true,
'no_blank_lines_after_phpdoc' => true,
'no_closing_tag' => true,
'no_empty_phpdoc' => true,
'no_empty_statement' => true,
'no_extra_consecutive_blank_lines' => true,
'no_leading_namespace_whitespace' => true,
'no_singleline_whitespace_before_semicolons' => true,
'no_spaces_after_function_name' => true,
'no_spaces_inside_parenthesis' => true,
'no_trailing_comma_in_list_call' => true,
'no_trailing_whitespace' => true,
'no_unused_imports' => true,
'no_whitespace_in_blank_line' => true,
'phpdoc_align' => true,
'phpdoc_indent' => true,
'phpdoc_no_access' => true,
'phpdoc_no_empty_return' => true,
'phpdoc_no_package' => true,
'phpdoc_scalar' => true,
'phpdoc_separation' => true,
'phpdoc_to_comment' => true,
'phpdoc_trim' => true,
'phpdoc_types' => true,
'phpdoc_var_without_name' => true,
'self_accessor' => true,
'simplified_null_return' => true,
'single_blank_line_at_eof' => true,
'single_import_per_statement' => true,
'single_line_after_imports' => true,
'single_quote' => true,
'ternary_operator_spaces' => true,
'trim_array_spaces' => true,
'visibility_required' => true,
]
)
->setFinder(
PhpCsFixer\Finder::create()
->files()
->in(__DIR__ . '/src')
->in(__DIR__ . '/tests')
->name('*.php')
);
26 changes: 26 additions & 0 deletions .travis.yml
@@ -0,0 +1,26 @@
language: php

php:
- 7.0
- 7.0snapshot
- 7.1
- 7.1snapshot
- master

sudo: false

before_install:
- composer self-update
- composer clear-cache

install:
- travis_retry composer update --no-interaction --no-ansi --no-progress --no-suggest --optimize-autoloader --prefer-stable

script:
- ./vendor/bin/phpunit --coverage-clover=coverage.xml

after_success:
- bash <(curl -s https://codecov.io/bash)

notifications:
email: false
7 changes: 7 additions & 0 deletions ChangeLog.md
@@ -0,0 +1,7 @@
# Change Log

All notable changes to `sebastianbergmann/object-reflector` are documented in this file using the [Keep a CHANGELOG](http://keepachangelog.com/) principles.

## 1.0.0 - 2017-03-12

* Initial release
33 changes: 33 additions & 0 deletions LICENSE
@@ -0,0 +1,33 @@
Object Reflector

Copyright (c) 2017, Sebastian Bergmann <sebastian@phpunit.de>.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.

* Neither the name of Sebastian Bergmann nor the names of his
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
14 changes: 14 additions & 0 deletions README.md
@@ -0,0 +1,14 @@
# Object Reflector

Allows reflection of object attributes, including inherited and non-public ones.

## Installation

You can add this library as a local, per-project dependency to your project using [Composer](https://getcomposer.org/):

composer require sebastian/object-reflector

If you only need this library during development, for instance to run your project's test suite, then you should add it as a development-time dependency:

composer require --dev sebastian/object-reflector

22 changes: 22 additions & 0 deletions build.xml
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="object-reflector" default="setup">
<target name="setup" depends="clean,composer"/>

<target name="clean" description="Cleanup build artifacts">
<delete dir="${basedir}/vendor"/>
<delete file="${basedir}/composer.lock"/>
</target>

<target name="composer" depends="clean" description="Install dependencies with Composer">
<exec executable="composer" taskname="composer">
<arg value="update"/>
<arg value="--no-interaction"/>
<arg value="--no-progress"/>
<arg value="--no-ansi"/>
<arg value="--no-suggest"/>
<arg value="--optimize-autoloader"/>
<arg value="--prefer-stable"/>
</exec>
</target>
</project>

33 changes: 33 additions & 0 deletions composer.json
@@ -0,0 +1,33 @@
{
"name": "sebastian/object-reflector",
"description": "Allows reflection of object attributes, including inherited and non-public ones",
"homepage": "https://github.com/sebastianbergmann/object-reflector/",
"license": "BSD-3-Clause",
"authors": [
{
"name": "Sebastian Bergmann",
"email": "sebastian@phpunit.de"
}
],
"require": {
"php": "^7.0"
},
"require-dev": {
"phpunit/phpunit": "^6.0"
},
"autoload": {
"classmap": [
"src/"
]
},
"autoload-dev": {
"classmap": [
"tests/_fixture/"
]
},
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
}
}
}
19 changes: 19 additions & 0 deletions phpunit.xml
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.0/phpunit.xsd"
bootstrap="vendor/autoload.php"
forceCoversAnnotation="true"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
verbose="true">
<testsuite>
<directory suffix="Test.php">tests</directory>
</testsuite>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>
17 changes: 17 additions & 0 deletions src/Exception.php
@@ -0,0 +1,17 @@
<?php
/*
* This file is part of object-reflector.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace SebastianBergmann\ObjectReflector;

interface Exception
{
}
17 changes: 17 additions & 0 deletions src/InvalidArgumentException.php
@@ -0,0 +1,17 @@
<?php
/*
* This file is part of object-reflector.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace SebastianBergmann\ObjectReflector;

class InvalidArgumentException extends \InvalidArgumentException implements Exception
{
}
57 changes: 57 additions & 0 deletions src/ObjectReflector.php
@@ -0,0 +1,57 @@
<?php
/*
* This file is part of object-reflector.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace SebastianBergmann\ObjectReflector;

class ObjectReflector
{
/**
* @param object $object
*
* @return array
*
* @throws InvalidArgumentException
*/
public function getAttributes($object): array
{
if (!is_object($object)) {
throw new InvalidArgumentException;
}

$attributes = [];
$reflector = new \ReflectionObject($object);

foreach ($reflector->getProperties() as $attribute) {
$attribute->setAccessible(true);

try {
$attributes[$attribute->getName()] = $attribute->getValue($object);
} catch (\Throwable $t) {
continue;
}
}

while ($reflector = $reflector->getParentClass()) {
foreach ($reflector->getProperties() as $attribute) {
$attribute->setAccessible(true);

try {
$attributes[$attribute->getDeclaringClass()->getName() . '::' . $attribute->getName()] = $attribute->getValue($object);
} catch (\Throwable $t) {
continue;
}
}
}

return $attributes;
}
}
45 changes: 45 additions & 0 deletions tests/ObjectReflectorTest.php
@@ -0,0 +1,45 @@
<?php
/*
* This file is part of object-reflector.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace SebastianBergmann\ObjectReflector;

use PHPUnit\Framework\TestCase;
use SebastianBergmann\ObjectReflector\TestFixture\ChildClass;

/**
* @covers SebastianBergmann\ObjectReflector\ObjectReflector
*/
class ObjectReflectorTest extends TestCase
{
/**
* @var ObjectReflector
*/
private $objectReflector;

protected function setUp()/*: void */
{
$this->objectReflector = new ObjectReflector;
}

public function testReflectsAttributesOfObject()/*: void */
{
$o = new ChildClass;

$this->assertEquals(
[
'foo' => 'baz',
'SebastianBergmann\ObjectReflector\TestFixture\ParentClass::foo' => 'bar'
],
$this->objectReflector->getAttributes($o)
);
}
}

0 comments on commit 2201553

Please sign in to comment.