Skip to content

Commit

Permalink
Fix CS
Browse files Browse the repository at this point in the history
  • Loading branch information
adrienlucas committed Aug 21, 2020
1 parent 3dfd877 commit 54f9c8b
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 36 deletions.
8 changes: 4 additions & 4 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
use Psalm\SymfonyPsalmPlugin\Handler\DoctrineRepositoryHandler;
use Psalm\SymfonyPsalmPlugin\Handler\HeaderBagHandler;
use Psalm\SymfonyPsalmPlugin\Symfony\ContainerMeta;
use Psalm\SymfonyPsalmPlugin\Twig\CachedTemplatesTainter;
use Psalm\SymfonyPsalmPlugin\Twig\AnalyzedTemplatesTainter;
use Psalm\SymfonyPsalmPlugin\Twig\CachedTemplatesTainter;
use SimpleXMLElement;
use Symfony\Component\HttpKernel\Kernel;

Expand Down Expand Up @@ -86,9 +86,9 @@ public function __invoke(RegistrationInterface $api, SimpleXMLElement $config =
$api->addStubFile($stubFilePath);
}

if(isset($config->twigCachePath)) {
static::$twig_cache_path = getcwd() . DIRECTORY_SEPARATOR . ltrim((string) $config->twigCachePath, DIRECTORY_SEPARATOR);
if(!is_dir(static::$twig_cache_path) || !is_readable(static::$twig_cache_path)) {
if (isset($config->twigCachePath)) {
static::$twig_cache_path = getcwd().DIRECTORY_SEPARATOR.ltrim((string) $config->twigCachePath, DIRECTORY_SEPARATOR);
if (!is_dir(static::$twig_cache_path) || !is_readable(static::$twig_cache_path)) {
throw new ConfigException(sprintf('The twig directory %s is missing or not readable.', static::$twig_cache_path));
}

Expand Down
9 changes: 3 additions & 6 deletions src/Test/CodeceptionModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

namespace Psalm\SymfonyPsalmPlugin\Test;


use Codeception\Exception\ModuleRequireException;
use Codeception\Module as BaseModule;
use Codeception\Module\Filesystem;
use Composer\InstalledVersions;
use Composer\Semver\VersionParser;
use PackageVersions\Versions;
Expand All @@ -31,13 +28,13 @@ class CodeceptionModule extends BaseModule
public function haveTheFollowingTemplate(string $templateName, string $code): void
{
$rootDirectory = rtrim($this->config['default_dir'], '/');
$templateRootDirectory = $rootDirectory . '/templates';
$templateRootDirectory = $rootDirectory.'/templates';
if (!file_exists($templateRootDirectory)) {
mkdir($templateRootDirectory);
}

file_put_contents(
$templateRootDirectory . '/' . $templateName,
$templateRootDirectory.'/'.$templateName,
$code
);
}
Expand All @@ -48,7 +45,7 @@ public function haveTheFollowingTemplate(string $templateName, string $code): vo
public function haveTheTemplateCompiled(string $templateName, string $cacheDirectory): void
{
$rootDirectory = rtrim($this->config['default_dir'], '/');
$cacheDirectory = $rootDirectory . '/' .ltrim($cacheDirectory, '/');
$cacheDirectory = $rootDirectory.'/'.ltrim($cacheDirectory, '/');
if (!file_exists($cacheDirectory)) {
mkdir($cacheDirectory, 0777, true);
}
Expand Down
8 changes: 2 additions & 6 deletions src/Test/TwigBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@

namespace Psalm\SymfonyPsalmPlugin\Test;


use InvalidArgumentException;
use Psalm\CodeLocation;
use RuntimeException;
use Twig\Cache\CacheInterface;
use Twig\Cache\FilesystemCache;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
Expand All @@ -20,11 +16,11 @@ class TwigBridge
/**
* @var Environment|null
*/
static private $environment;
private static $environment;

public static function getEnvironment(string $rootDirectory, string $cacheDirectory): Environment
{
if(self::$environment !== null){
if (null !== self::$environment) {
return self::$environment;
}

Expand Down
7 changes: 3 additions & 4 deletions src/Twig/AnalyzedTemplatesTainter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Psalm\SymfonyPsalmPlugin\Twig;


use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\MethodCall;
Expand All @@ -25,8 +24,8 @@ class AnalyzedTemplatesTainter implements AfterMethodCallAnalysisInterface
public static function afterMethodCallAnalysis(Expr $expr, string $method_id, string $appearing_method_id, string $declaring_method_id, Context $context, StatementsSource $statements_source, Codebase $codebase, array &$file_replacements = [], Union &$return_type_candidate = null): void
{
if (
$codebase->taint === null
|| !$expr instanceof MethodCall || $method_id !== Environment::class . '::render' || empty($expr->args)
null === $codebase->taint
|| !$expr instanceof MethodCall || $method_id !== Environment::class.'::render' || empty($expr->args)
|| !isset($expr->args[0]->value) || !$expr->args[0]->value instanceof String_
|| !isset($expr->args[1]->value) || !$expr->args[1]->value instanceof Array_
) {
Expand All @@ -36,7 +35,7 @@ public static function afterMethodCallAnalysis(Expr $expr, string $method_id, st
$template_name = $expr->args[0]->value->value;
$twig_arguments_type = $statements_source->getNodeTypeProvider()->getType($expr->args[1]->value);

if ($twig_arguments_type === null || $twig_arguments_type->parent_nodes === null) {
if (null === $twig_arguments_type || null === $twig_arguments_type->parent_nodes) {
return;
}

Expand Down
9 changes: 4 additions & 5 deletions src/Twig/CachedTemplatesTainter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Psalm\SymfonyPsalmPlugin\Twig;


use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
Expand Down Expand Up @@ -35,11 +34,11 @@ public static function getClassLikeNames(): array

public static function getMethodReturnType(StatementsSource $source, string $fq_classlike_name, string $method_name_lowercase, array $call_args, Context $context, CodeLocation $code_location, array $template_type_parameters = null, string $called_fq_classlike_name = null, string $called_method_name_lowercase = null): void
{
if(!$source instanceof StatementsAnalyzer) {
if (!$source instanceof StatementsAnalyzer) {
throw new RuntimeException(sprintf('The %s::%s hook can only be called using a %s.', __CLASS__, __METHOD__, StatementsAnalyzer::class));
}

if ($method_name_lowercase !== 'render') {
if ('render' !== $method_name_lowercase) {
return;
}

Expand All @@ -54,14 +53,14 @@ public static function getMethodReturnType(StatementsSource $source, string $fq_
);

$firstArgument = $call_args[0]->value;
if(!$firstArgument instanceof String_) {
if (!$firstArgument instanceof String_) {
return;
}

$template = self::getTemplate($source->getCodebase()->config, $firstArgument->value);

$context->vars_in_scope['$__fake_twig_env_var__'] = new Union([
new TNamedObject(get_class($template))
new TNamedObject(get_class($template)),
]);

MethodCallAnalyzer::analyze(
Expand Down
7 changes: 3 additions & 4 deletions src/Twig/TemplateFileAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Psalm\SymfonyPsalmPlugin\Twig;


use Psalm\Context;
use Psalm\Internal\Analyzer\FileAnalyzer;
use Psalm\Internal\Codebase\Taint;
Expand All @@ -26,7 +25,7 @@ public function analyze(
) {
$codebase = $this->project_analyzer->getCodebase();

if($codebase->taint === null) {
if (null === $codebase->taint) {
return;
}

Expand Down Expand Up @@ -54,7 +53,7 @@ public function analyze(
$taint_source = self::getTaintNodeForTwigNamedVariable($local_file_name, $var_name);
$this->taint->addTaintNode($taint_source);

if ($var_type->parent_nodes === null) {
if (null === $var_type->parent_nodes) {
continue;
}

Expand All @@ -68,7 +67,7 @@ public static function getTaintNodeForTwigNamedVariable(
string $template_id,
string $variable_name
): TaintNode {
$label = $arg_id = strtolower($template_id) . '#' . strtolower($variable_name);
$label = $arg_id = strtolower($template_id).'#'.strtolower($variable_name);

return new TaintNode($arg_id, $label, null, null);
}
Expand Down
14 changes: 7 additions & 7 deletions src/Twig/TwigModuleAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private function analyzePrintNode(PrintNode $node): Taintable
$sink->taints = [
TaintKind::INPUT_HTML,
TaintKind::USER_SECRET,
TaintKind::SYSTEM_SECRET
TaintKind::SYSTEM_SECRET,
];

$this->taint->addSink($sink);
Expand All @@ -95,7 +95,7 @@ private function analyzePrintNode(PrintNode $node): Taintable
throw new RuntimeException('The expr node has an expected type.');
}

if($expression_taint = $this->analyzeExpression($expression)) {
if ($expression_taint = $this->analyzeExpression($expression)) {
$this->taint->addPath($expression_taint, $sink, 'arg');
}

Expand All @@ -106,14 +106,14 @@ private function analyzeExpression(AbstractExpression $expression): ?Taintable
{
if ($expression instanceof FilterExpression) {
return $this->analyzeFilter($expression);
} else {
return null;
}

return null;
}

private function analyzeFilter(FilterExpression $expression): ?Taintable
{
if ($expression->getNode('filter')->getAttribute('value') !== 'raw') {
if ('raw' !== $expression->getNode('filter')->getAttribute('value')) {
return null;
}

Expand Down Expand Up @@ -155,7 +155,7 @@ private function analyzeName(NameExpression $expression): Taintable
return $variable_taint;
}

private static function getLocation(Source $sourceContext, int $lineNumber) : CodeLocation
private static function getLocation(Source $sourceContext, int $lineNumber): CodeLocation
{
$fileName = $sourceContext->getName();
$filePath = $sourceContext->getPath();
Expand All @@ -166,7 +166,7 @@ private static function getLocation(Source $sourceContext, int $lineNumber) : Co

$file_start = 0;

for ($i = 0; $i < $lineNumber - 1; $i++) {
for ($i = 0; $i < $lineNumber - 1; ++$i) {
$file_start += strlen($lines[$i]) + 1;
}

Expand Down

0 comments on commit 54f9c8b

Please sign in to comment.