Skip to content
This repository has been archived by the owner on May 6, 2018. It is now read-only.

Commit

Permalink
Merge pull request #1 from lcobucci/implement-library
Browse files Browse the repository at this point in the history
Implement library
  • Loading branch information
lcobucci authored May 1, 2018
2 parents c7fa9a9 + 5b67e8a commit 45d4a0c
Show file tree
Hide file tree
Showing 44 changed files with 2,690 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/tests export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.scrutinizer.yml export-ignore
/.travis.yml export-ignore
/infection.json.dist export-ignore
/phpcs.xml.dist export-ignore
/phpstan.neon.dist export-ignore
/phpunit.xml.dist export-ignore
/README.md export-ignore
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/vendor
/phpunit.xml
/phpcs.xml
/.phpcs.cache
/composer.lock
/infection-log.txt
37 changes: 37 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
build:
nodes:
analysis:
environment:
mysql: false
postgresql: false
redis: false
rabbitmq: false
mongodb: false
php:
version: 7.2
cache:
disabled: false
directories:
- ~/.composer/cache

project_setup:
override: true
tests:
override:
- php-scrutinizer-run
- phpcs-run

dependencies:
override:
- composer install --no-interaction --prefer-dist

checks:
php: true

tools:
external_code_coverage: true

build_failure_conditions:
- 'elements.rating(<= C).new.exists'
- 'issues.severity(>= MAJOR).new.exists'
- 'project.metric_change("scrutinizer.test_coverage", < -0.01)'
55 changes: 55 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
dist: trusty
sudo: false
language: php

php:
- 7.2
- nightly

cache:
directories:
- $HOME/.composer/cache

before_install:
- mv ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini{,.disabled} || echo "xdebug not available"
- composer self-update

install: travis_retry composer install

script:
- ./vendor/bin/phpunit

jobs:
allow_failures:
- php: nightly
- env: STATIC_ANALYSIS=1

include:
- stage: Code Quality
env: TEST_COVERAGE=1
before_script:
- mv ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini{.disabled,}
- if [[ ! $(php -m | grep -si xdebug) ]]; then echo "xdebug required for coverage"; exit 1; fi
script:
- ./vendor/bin/phpunit --testsuite unit --coverage-clover ./clover.xml
after_script:
- wget https://scrutinizer-ci.com/ocular.phar
- php ocular.phar code-coverage:upload --format=php-clover ./clover.xml

- stage: Code Quality
env: CODE_STANDARD=1
script:
- ./vendor/bin/phpcs

- stage: Code Quality
env: STATIC_ANALYSIS=1
script:
- ./vendor/bin/phpstan analyse

- stage: Code Quality
env: MUTATION_TESTS=1
before_script:
- mv ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini{.disabled,}
- if [[ ! $(php -m | grep -si xdebug) ]]; then echo "xdebug required for mutation tests"; exit 1; fi
script:
- ./vendor/bin/infection --test-framework-options='--testsuite unit' --threads=4 --min-msi=100 --min-covered-msi=100
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ you can install it using [Composer](http://getcomposer.org).
composer require lcobucci/chimera-mapping
```

### PHP Configuration

In order to make sure that we're dealing with the correct data, we're using `assert()`,
which is a very interesting feature in PHP but not often used. The nice thing
about `assert()` is that we can (and should) disable it in production mode so
that we don't have useless statements.

So, for production mode, we recommend you to set `zend.assertions` to `-1` in your `php.ini`.
For development you should leave `zend.assertions` as `1` and set `assert.exception` to `1`, which
will make PHP throw an [`AssertionError`](https://secure.php.net/manual/en/class.assertionerror.php)
when things go wrong.

Check the documentation for more information: https://secure.php.net/manual/en/function.assert.php

## Usage

These are the annotations related to how services should be mapped to the
Expand Down
45 changes: 45 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "lcobucci/chimera-mapping",
"description": "Useful annotations to configure the application layer of your software",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Luís Cobucci",
"email": "[email protected]"
}
],
"config": {
"preferred-install": "dist",
"sort-packages": true
},
"require": {
"php": "^7.2",
"doctrine/annotations": "^1.6"
},
"require-dev": {
"doctrine/coding-standard": "^4.0",
"infection/infection": "^0.8",
"phpstan/phpdoc-parser": "^0.3@dev",
"phpstan/phpstan": "^0.10@dev",
"phpstan/phpstan-phpunit": "^0.10@dev",
"phpstan/phpstan-strict-rules": "^0.10@dev",
"phpunit/phpunit": "^7.1",
"squizlabs/php_codesniffer": "^3.2"
},
"autoload": {
"psr-4": {
"Lcobucci\\Chimera\\Mapping\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Lcobucci\\Chimera\\Mapping\\Tests\\": "tests"
}
},
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
}
}
11 changes: 11 additions & 0 deletions infection.json.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"timeout": 10,
"source": {
"directories": [
"src"
]
},
"logs": {
"text": "infection-log.txt"
}
}
40 changes: 40 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0"?>
<ruleset>
<arg name="basepath" value="." />
<arg name="extensions" value="php" />
<arg name="parallel" value="80" />
<arg name="colors" />
<arg name="cache" value=".phpcs.cache" />
<arg value="p" />

<file>src</file>
<file>tests</file>

<rule ref="Doctrine">
<exclude name="SlevomatCodingStandard.Commenting.RequireOneLinePropertyDocComment" />
</rule>

<rule ref="SlevomatCodingStandard.TypeHints.DeclareStrictTypes">
<properties>
<property name="newlinesCountBetweenOpenTagAndDeclare" value="1"/>
<property name="spacesCountAroundEqualsSign" value="0"/>
<property name="newlinesCountAfterDeclare" value="2"/>
</properties>
</rule>

<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHintSpacing">
<properties>
<property name="spacesCountBeforeColon" value="0"/>
</properties>
</rule>

<rule ref="SlevomatCodingStandard.Commenting.DisallowOneLinePropertyDocComment" />

<rule ref="PSR1.Classes.ClassDeclaration.MultipleClasses">
<exclude-pattern>tests/Functional</exclude-pattern>
</rule>

<rule ref="Squiz.Classes.ClassFileName.NoMatch">
<exclude-pattern>tests/Functional</exclude-pattern>
</rule>
</ruleset>
10 changes: 10 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
includes:
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-phpunit/rules.neon
- vendor/phpstan/phpstan-strict-rules/rules.neon

parameters:
level: 7
paths:
- src
- tests
29 changes: 29 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
colors="true"
verbose="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
beStrictAboutChangesToGlobalState="true"
beStrictAboutCoversAnnotation="true"
beStrictAboutResourceUsageDuringSmallTests="true"
forceCoversAnnotation="true"
>
<testsuites>
<testsuite name="unit">
<directory>tests/Unit</directory>
</testsuite>

<testsuite name="functional">
<directory>tests/Functional</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>src</directory>
</whitelist>
</filter>
</phpunit>
14 changes: 14 additions & 0 deletions src/Annotation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
declare(strict_types=1);

namespace Lcobucci\Chimera\Mapping;

use Doctrine\Common\Annotations\AnnotationException;

interface Annotation
{
/**
* @throws AnnotationException
*/
public function validate(string $context): void;
}
68 changes: 68 additions & 0 deletions src/Reader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
declare(strict_types=1);

namespace Lcobucci\Chimera\Mapping;

use Doctrine\Common\Annotations\AnnotationException;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\Reader as ReaderInterface;
use ReflectionClass;
use function assert;

final class Reader
{
/**
* @var ReaderInterface
*/
private $decorated;

public function __construct(ReaderInterface $reader)
{
$this->decorated = $reader;
}

/**
* @throws AnnotationException
*/
public static function fromDefault(): self
{
return new self(new AnnotationReader());
}

/**
* @return Annotation[]
*
* @throws AnnotationException
*/
public function getClassAnnotations(ReflectionClass $class): array
{
$annotations = [];

foreach ($this->decorated->getClassAnnotations($class) as $annotation) {
if (! $annotation instanceof Annotation) {
continue;
}

$annotation->validate('class ' . $class->getName());

$annotations[] = $annotation;
}

return $annotations;
}

/**
* @throws AnnotationException
*/
public function getClassAnnotation(ReflectionClass $class, string $annotationName): ?Annotation
{
$annotation = $this->decorated->getClassAnnotation($class, $annotationName);
assert($annotation instanceof Annotation || $annotation === null);

if ($annotation instanceof Annotation) {
$annotation->validate('class ' . $class->getName());
}

return $annotation;
}
}
Loading

0 comments on commit 45d4a0c

Please sign in to comment.