initial commit

This commit is contained in:
Farhad Safarov 2019-11-12 16:36:05 +03:00
commit 378289cbe1
9 changed files with 179 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
vendor
composer.lock

25
.travis.yml Normal file
View File

@ -0,0 +1,25 @@
language: php
php:
- 7.1
- 7.2
- 7.3
before_install:
- phpenv config-rm xdebug.ini || true
install:
- composer install
- if [[ "$DEPS" = 'high' ]]; then travis_retry composer $DEFAULT_COMPOSER_FLAGS update; fi
- if [[ "$DEPS" = 'low' ]]; then travis_retry composer $DEFAULT_COMPOSER_FLAGS --prefer-lowest --prefer-stable update; fi
- if [[ "$DEPS" = 'stable' ]]; then travis_retry composer $DEFAULT_COMPOSER_FLAGS --prefer-stable update; fi
script: composer check
env:
matrix:
- DEPS="low"
- DEPS="high"
- DEPS="stable"
global:
- DEFAULT_COMPOSER_FLAGS="--no-interaction --no-suggest"

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2019 Farhad Safarov
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

8
README.md Normal file
View File

@ -0,0 +1,8 @@
# Symfony Psalm Plugin
### Installation
```
composer require --dev seferov/symfony-psalm-plugin
vendor/bin/psalm-plugin enable seferov/symfony-psalm-plugin
```

33
composer.json Normal file
View File

@ -0,0 +1,33 @@
{
"name": "seferov/symfony-psalm-plugin",
"description": "Psalm Plugin for Symfony",
"type": "psalm-plugin",
"require": {
"php": "^7.1",
"vimeo/psalm": "^3.2",
"symfony/framework-bundle": "^3.0 || ^4.0"
},
"license": "MIT",
"authors": [
{
"name": "Farhad Safarov",
"email": "farhad.safarov@gmail.com"
}
],
"autoload": {
"psr-4": {
"Seferov\\SymfonyPsalmPlugin\\": "src"
}
},
"extra": {
"psalm" : {
"pluginClass": "Seferov\\SymfonyPsalmPlugin\\Plugin"
}
},
"scripts": {
"check": [
"@analyze"
],
"analyze": "psalm"
}
}

15
psalm.xml Normal file
View File

@ -0,0 +1,15 @@
<?xml version="1.0"?>
<psalm
totallyTyped="false"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
</psalm>

View File

@ -0,0 +1,42 @@
<?php
namespace Seferov\SymfonyPsalmPlugin\Handler;
use PhpParser\Node;
use Psalm\Codebase;
use Psalm\CodeLocation;
use Psalm\FileManipulation;
use Psalm\IssueBuffer;
use Psalm\Plugin\Hook\AfterClassLikeAnalysisInterface;
use Psalm\StatementsSource;
use Psalm\Storage\ClassLikeStorage;
use Seferov\SymfonyPsalmPlugin\Issue\ContainerDependency;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ClassHandler implements AfterClassLikeAnalysisInterface
{
/**
* Called after a statement has been checked
*
* @param FileManipulation[] $file_replacements
*
* @return null|false
*/
public static function afterStatementAnalysis(Node\Stmt\ClassLike $stmt, ClassLikeStorage $classlike_storage, StatementsSource $statements_source, Codebase $codebase, array &$file_replacements = [])
{
foreach ($stmt->stmts as $stmt) {
if ($stmt instanceof Node\Stmt\ClassMethod && '__construct' === $stmt->name->name) {
foreach ($stmt->params as $param) {
if ($param->type instanceof Node\Name && ContainerInterface::class === $param->type->getAttributes()['resolvedName']) {
IssueBuffer::accepts(
new ContainerDependency(new CodeLocation($statements_source, $stmt)),
$statements_source->getSuppressedIssues()
);
}
}
}
}
return null;
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace Seferov\SymfonyPsalmPlugin\Issue;
use Psalm\CodeLocation;
use Psalm\Issue\CodeIssue;
class ContainerDependency extends CodeIssue
{
public function __construct(CodeLocation $code_location)
{
parent::__construct('Container must not inject into services as dependency!', $code_location);
}
}

19
src/Plugin.php Normal file
View File

@ -0,0 +1,19 @@
<?php
namespace Seferov\SymfonyPsalmPlugin;
use Psalm\Plugin\PluginEntryPointInterface;
use Psalm\Plugin\RegistrationInterface;
use Seferov\SymfonyPsalmPlugin\Handler\ClassHandler;
use SimpleXMLElement;
class Plugin implements PluginEntryPointInterface
{
/** @return void */
public function __invoke(RegistrationInterface $api, SimpleXMLElement $config = null)
{
require_once __DIR__ . '/Handler/ClassHandler.php';
$api->registerHooksFromClass(ClassHandler::class);
}
}