apply thecodingmachine/safe

This commit is contained in:
Tomas Votruba 2018-09-21 07:12:24 +02:00
parent 1ab794a3b9
commit f07480c773
43 changed files with 106 additions and 58 deletions

View File

@ -19,7 +19,8 @@
"symfony/dependency-injection": "^3.4|^4.1",
"symfony/finder": "^3.4|^4.1",
"symplify/better-phpdoc-parser": "^5.0",
"symplify/package-builder": "^5.0"
"symplify/package-builder": "^5.0",
"thecodingmachine/safe": "^0.1.4"
},
"require-dev": {
"humbug/php-scoper": "^0.9.2",
@ -28,6 +29,8 @@
"symplify/easy-coding-standard": "^5.0",
"symplify/monorepo-builder": "^5.0",
"symplify/phpstan-extensions": "^5.0",
"thecodingmachine/phpstan-safe-rule": "^0.1.0",
"thecodingmachine/phpstan-strict-rules": "^0.10.3",
"tracy/tracy": "^2.5"
},
"autoload": {

View File

@ -4,6 +4,7 @@ namespace Rector\ConsoleDiffer\Console\Formatter;
use Nette\Utils\Strings;
use Symfony\Component\Console\Formatter\OutputFormatter;
use function Safe\sprintf;
/**
* Most is copy-pasted from https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/master/src/Differ/DiffConsoleFormatter.php
@ -20,13 +21,9 @@ final class DiffConsoleFormatter
public function __construct()
{
$this->template = sprintf(
'<comment> ---------- begin diff ----------</comment>' .
'%s%%s%s' .
'<comment> ----------- end diff -----------</comment>',
PHP_EOL,
PHP_EOL
);
$this->template = sprintf('<comment> ---------- begin diff ----------</comment>' .
'%s%%s%s' .
'<comment> ----------- end diff -----------</comment>', PHP_EOL, PHP_EOL);
}
public function format(string $diff): string
@ -36,24 +33,21 @@ final class DiffConsoleFormatter
private function formatWithTemplate(string $diff, string $template): string
{
return sprintf(
$template,
implode(PHP_EOL, array_map(function ($string) {
// make "+" lines green
// make "-" lines red
// make "@ note" lines cyan
$string = preg_replace(
['/^(\+.*)/', '/^(\-.*)/', '/^(@.*)/'],
['<fg=green>\1</fg=green>', '<fg=red>\1</fg=red>', '<fg=cyan>\1</fg=cyan>'],
$string
);
return sprintf($template, implode(PHP_EOL, array_map(function ($string) {
// make "+" lines green
// make "-" lines red
// make "@ note" lines cyan
$string = preg_replace(
['/^(\+.*)/', '/^(\-.*)/', '/^(@.*)/'],
['<fg=green>\1</fg=green>', '<fg=red>\1</fg=red>', '<fg=cyan>\1</fg=cyan>'],
$string
);
if ($string === ' ') {
$string = rtrim($string);
}
if ($string === ' ') {
$string = rtrim($string);
}
return $string;
}, Strings::split(OutputFormatter::escape(rtrim($diff)), "#\n\r|\n#")))
);
return $string;
}, Strings::split(OutputFormatter::escape(rtrim($diff)), "#\n\r|\n#"))));
}
}

View File

@ -10,6 +10,7 @@ use Rector\NodeAnalyzer\MethodCallAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
use function Safe\sprintf;
final class AliasToClassRector extends AbstractRector
{

View File

@ -8,6 +8,7 @@ use PHPStan\Analyser\ScopeFactory;
use PHPStan\Analyser\TypeSpecifier;
use PHPStan\Broker\Broker;
use PHPStan\DependencyInjection\ContainerFactory;
use function Safe\getcwd;
final class PHPStanServicesFactory
{

View File

@ -11,6 +11,7 @@ use Rector\PhpParser\CurrentNodeProvider;
use Symplify\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Symplify\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Symplify\BetterPhpDocParser\Printer\PhpDocInfoPrinter;
use function Safe\sprintf;
final class DocBlockAnalyzer
{

View File

@ -8,6 +8,7 @@ use PhpParser\Node;
use PhpParser\Node\Stmt\Use_;
use Rector\NodeTypeResolver\Node\Attribute;
use Rector\Php\TypeAnalyzer;
use function Safe\sprintf;
final class NamespaceAnalyzer
{

View File

@ -6,6 +6,7 @@ use PhpParser\Comment\Doc;
use PhpParser\Node\Scalar\String_;
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockAnalyzer;
use Rector\NodeTypeResolver\Tests\AbstractNodeTypeResolverContainerAwareTestCase;
use function Safe\sprintf;
final class DocBlockAnalyzerTest extends AbstractNodeTypeResolverContainerAwareTestCase
{

View File

@ -13,6 +13,9 @@ use Rector\Rector\AbstractPHPUnitRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
/**
* @see https://thephp.cc/news/2016/02/questioning-phpunit-best-practices
*/
final class ExceptionAnnotationRector extends AbstractPHPUnitRector
{
/**

View File

@ -6,6 +6,7 @@ use Nette\Utils\Strings;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Exception\ShouldNotHappenException;
use Rector\NodeTypeResolver\Node\Attribute;
use function Safe\sprintf;
final class TemplateGuesser
{

View File

@ -10,6 +10,8 @@ use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symplify\PackageBuilder\Parameter\ParameterProvider;
use Throwable;
use function Safe\class_implements;
use function Safe\sprintf;
final class DefaultAnalyzedSymfonyApplicationContainer implements AnalyzedApplicationContainerInterface
{
@ -52,7 +54,7 @@ final class DefaultAnalyzedSymfonyApplicationContainer implements AnalyzedApplic
throw new ShouldNotHappenException(sprintf(
'Service type for "%s" name was not found in container of your Symfony application.',
$name
));
), $throwable->getCode(), $throwable);
}
$serviceClass = get_class($service);

View File

@ -5,6 +5,7 @@ namespace Rector\Symfony\Bridge;
use Rector\Configuration\Option;
use Rector\Exception\Configuration\InvalidConfigurationException;
use Symfony\Component\HttpKernel\Kernel;
use function Safe\sprintf;
final class SymfonyKernelParameterGuard
{

View File

@ -4,6 +4,7 @@ namespace Rector\Symfony\Tests\FrameworkBundle\GetToConstructorInjectionRector;
use Rector\Exception\Configuration\InvalidConfigurationException;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use function Safe\sprintf;
/**
* @covers \Rector\Symfony\Rector\FrameworkBundle\GetToConstructorInjectionRector

View File

@ -6,6 +6,7 @@ use Rector\Exception\Configuration\InvalidConfigurationException;
use Rector\Symfony\Tests\FrameworkBundle\AbstractToConstructorInjectionRectorSource\SomeNonKernelClass;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symfony\Component\HttpKernel\Kernel;
use function Safe\sprintf;
/**
* @covers \Rector\Symfony\Rector\FrameworkBundle\GetToConstructorInjectionRector
@ -15,12 +16,14 @@ final class ThirdInvalidConfigurationConstructorInjectionRectorTest extends Abst
public function test(): void
{
$this->expectException(InvalidConfigurationException::class);
$this->expectExceptionMessage(sprintf(
'Kernel class "%s" provided in "parameters > %s" is not instance of "%s". Make sure it is.',
SomeNonKernelClass::class,
'kernel_class',
Kernel::class
));
$this->expectExceptionMessage(
sprintf(
'Kernel class "%s" provided in "parameters > %s" is not instance of "%s". Make sure it is.',
SomeNonKernelClass::class,
'kernel_class',
Kernel::class
)
);
$this->doTestFileMatchesExpectedContent(__DIR__ . '/Wrong/wrong.php.inc', __DIR__ . '/Correct/correct.php.inc');
}

View File

@ -5,6 +5,8 @@ namespace Rector\Utils;
use Nette\Utils\Strings;
use Rector\Exception\FileSystem\DirectoryNotFoundException;
use Rector\Exception\FileSystem\FileNotFoundException;
use function Safe\glob;
use function Safe\sprintf;
final class FilesystemTweaker
{

View File

@ -13,6 +13,7 @@ use PhpParser\ParserFactory;
use PHPUnit\Framework\TestCase;
use Rector\NodeTypeResolver\NodeVisitor\ParentAndNextNodeVisitor;
use Rector\Utils\BetterNodeFinder;
use function Safe\file_get_contents;
final class BetterNodeFinderTest extends TestCase
{

View File

@ -7,6 +7,7 @@ use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
use Rector\YamlRector\Contract\YamlRectorInterface;
use Rector\YamlRector\PathResolver;
use function Safe\sprintf;
final class RenameSubKeyYamlRector implements YamlRectorInterface
{

View File

@ -7,6 +7,7 @@ use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
use Rector\YamlRector\Contract\YamlRectorInterface;
use Rector\YamlRector\PathResolver;
use function Safe\sprintf;
final class ReplaceValueYamlRector implements YamlRectorInterface
{

View File

@ -8,6 +8,7 @@ use Rector\DependencyInjection\ContainerFactory;
use Rector\FileSystem\FileGuard;
use Rector\YamlRector\YamlFileProcessor;
use Symfony\Component\Finder\SplFileInfo;
use function Safe\sprintf;
abstract class AbstractYamlRectorTest extends TestCase
{
@ -44,10 +45,11 @@ abstract class AbstractYamlRectorTest extends TestCase
$reconstructedFileContent = $this->yamlFileProcessor->processFileInfo(new SplFileInfo($file, '', ''));
$this->assertStringEqualsFile($reconstructedFile, $reconstructedFileContent, sprintf(
'Original file "%s" did not match the result.',
$file
));
$this->assertStringEqualsFile(
$reconstructedFile,
$reconstructedFileContent,
sprintf('Original file "%s" did not match the result.', $file)
);
}
abstract protected function provideConfig(): string;

View File

@ -1,9 +1,12 @@
includes:
- 'vendor/symplify/phpstan-extensions/config/config.neon'
- 'vendor/thecodingmachine/phpstan-strict-rules/phpstan-strict-rules.neon'
- 'vendor/thecodingmachine/phpstan-safe-rule/phpstan-safe-rule.neon'
parameters:
excludes_analyse:
# test files
- '*packages/NodeTypeResolver/tests/Source/AnotherClass.php'
- '*tests/Rector/MethodCall/MethodNameReplacerRector/**/SomeClass.php'
- '*packages/BetterReflection/tests/Reflector/NotLoadedSource/SomeClass.php'
- 'packages/NodeTypeResolver/tests/PerNodeTypeResolver/VariableTypeResolver/Source/NewClass.php'
@ -58,10 +61,11 @@ parameters:
# Error php-parser token
- '#Access to an undefined property PhpParser\\Node\\Expr\\Error\|PhpParser\\Node\\Expr\\Variable::\$name#'
# SplFileInfo::getRealPath() false positive
- '#Parameter \#1 \$pattern of function fnmatch expects string, string\|false given#'
- '#Parameter \#1 \$file of function file_put_contents expects string, string\|false given#'
# SplFileInfo::getRealPath() strict
- '#Parameter \#1 \$filePath of method Rector\\Parser\\Parser::parseFile\(\) expects string, string\|false given#'
- '#Parameter \#2 \$filename of function fnmatch expects string, string\|false given#'
- '#Parameter \#1 \$filename of function Safe\\file_put_contents expects string, string\|false given#'
- '#Parameter \#2 \$args of function Safe\\sprintf expects float\|int\|string\|null, string\|false given#'
# false positive, has annotation type above (@todo recheck for possible ignored positives)
- '#Access to an undefined property PhpParser\\Node::\$name#' # 11
@ -89,8 +93,9 @@ parameters:
- '#Access to an undefined property PhpParser\\Node\\Expr::\$value#' # 2
- '#Access to an undefined property PhpParser\\Node\\Expr::\$(name|var)#' # 2
# tests - known values
- '#Parameter \#1 \$code of method PhpParser\\Parser::parse\(\) expects string, string\|false given#' # 1
# variadic false positive
- '#In method "Rector\\Node\\NodeFactory::createArray", parameter \$items can be type-hinted to "array"#'
- '#In method "Rector\\Builder\\StatementGlue::addStatementToClassBeforeTypes", parameter \$types type is "array". Please provide a \@param annotation to further specify the type of the array. For instance: \@param int\[\] \$types#'
# too strict
- '#Parameter \#1 \$currentWorkingDirectory of class PHPStan\\DependencyInjection\\ContainerFactory constructor expects string, string\|false given#' # 1
# intentionally
- '#Empty catch block#'

View File

@ -108,10 +108,10 @@ final class AdditionalAutoloader
$robotLoader->addDirectory($autoloadDirectory);
}
// sometimes tests can include ambiguous classes
try {
$robotLoader->register();
} catch (Throwable $throwable) {
// sometimes tests can include ambiguous classes
}
}

View File

@ -4,6 +4,7 @@ namespace Rector\Bridge\Doctrine;
use Nette\Utils\Strings;
use Rector\Bridge\Contract\DoctrineEntityAndRepositoryMapperInterface;
use function Safe\substr;
final class DefaultDoctrineEntityAndRepositoryMapper implements DoctrineEntityAndRepositoryMapperInterface
{

View File

@ -10,6 +10,7 @@ use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Exception\NodeChanger\NodeMissingIdentifierException;
use function Safe\sprintf;
/**
* This class renames node identifier, e.g. ClassMethod rename:

View File

@ -8,6 +8,7 @@ use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property as PhpParserProperty;
use Rector\Builder\Class_\VariableInfo;
use Rector\Php\TypeAnalyzer;
use function Safe\sprintf;
/**
* This class builds properties, and adds them to a class

View File

@ -3,6 +3,7 @@
namespace Rector\Configuration\Rector\Guard;
use Rector\Exception\Rector\InvalidRectorConfigurationException;
use function Safe\sprintf;
final class RecipeGuard
{

View File

@ -6,6 +6,7 @@ use Jean85\PrettyVersions;
use Symfony\Component\Console\Application as SymfonyApplication;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputOption;
use function Safe\getcwd;
final class Application extends SymfonyApplication
{

View File

@ -16,6 +16,8 @@ use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Yaml\Yaml;
use Symplify\PackageBuilder\Console\Command\CommandNaming;
use function Safe\ksort;
use function Safe\sprintf;
final class GenerateRectorOverviewCommand extends Command
{

View File

@ -11,6 +11,8 @@ use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;
use Symplify\PackageBuilder\Console\Command\CommandNaming;
use function Safe\sort;
use function Safe\sprintf;
final class LevelsCommand extends Command
{

View File

@ -26,6 +26,7 @@ use Symfony\Component\Process\Process;
use Symplify\PackageBuilder\Console\Command\CommandNaming;
use Symplify\PackageBuilder\Parameter\ParameterProvider;
use Throwable;
use function Safe\sprintf;
final class ProcessCommand extends Command
{

View File

@ -3,6 +3,7 @@
namespace Rector\Console;
use Symfony\Component\Console\Style\SymfonyStyle;
use function Safe\sprintf;
final class ConsoleStyle extends SymfonyStyle
{

View File

@ -8,6 +8,7 @@ use Rector\Contract\Rector\RectorInterface;
use Rector\NodeTraverser\RectorNodeTraverser;
use Rector\Reporting\FileDiff;
use Rector\YamlRector\YamlFileProcessor;
use function Safe\sprintf;
final class ProcessCommandReporter
{
@ -62,11 +63,9 @@ final class ProcessCommandReporter
return;
}
$this->consoleStyle->title(sprintf(
'%d Changed file%s',
count($changedFiles),
count($changedFiles) === 1 ? '' : 's'
));
$this->consoleStyle->title(
sprintf('%d Changed file%s', count($changedFiles), count($changedFiles) === 1 ? '' : 's')
);
$this->consoleStyle->listing($changedFiles);
}
@ -79,11 +78,9 @@ final class ProcessCommandReporter
return;
}
$this->consoleStyle->title(sprintf(
'%d file%s with changes',
count($fileDiffs),
count($fileDiffs) === 1 ? '' : 's'
));
$this->consoleStyle->title(
sprintf('%d file%s with changes', count($fileDiffs), count($fileDiffs) === 1 ? '' : 's')
);
$i = 0;
foreach ($fileDiffs as $fileDiff) {

View File

@ -3,6 +3,7 @@
namespace Rector\DependencyInjection;
use Psr\Container\ContainerInterface;
use function Safe\putenv;
final class ContainerFactory
{

View File

@ -5,6 +5,7 @@ namespace Rector\Exception\Application;
use Exception;
use SplFileInfo;
use Throwable;
use function Safe\sprintf;
final class FileProcessingException extends Exception
{

View File

@ -3,6 +3,7 @@
namespace Rector\FileSystem;
use Rector\Exception\FileSystem\FileNotFoundException;
use function Safe\sprintf;
final class FileGuard
{

View File

@ -26,6 +26,7 @@ use Rector\Builder\Class_\VariableInfo;
use Rector\Exception\NotImplementedException;
use Rector\NodeTypeResolver\Node\Attribute;
use Rector\Php\TypeAnalyzer;
use function Safe\sprintf;
final class NodeFactory
{

View File

@ -8,6 +8,7 @@ use PhpParser\Node\Stmt\ClassConst;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use Rector\Exception\InvalidNodeTypeException;
use function Safe\sprintf;
final class VisibilityModifier
{

View File

@ -6,6 +6,7 @@ use PhpParser\Node\Expr\Yield_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\PrettyPrinter\Standard;
use Rector\NodeTypeResolver\Node\Attribute;
use function Safe\sprintf;
final class BetterStandardPrinter extends Standard
{

View File

@ -5,6 +5,7 @@ namespace Rector\Printer;
use PhpParser\Node;
use PhpParser\PrettyPrinter\Standard;
use SplFileInfo;
use function Safe\file_put_contents;
final class FormatPerservingPrinter
{

View File

@ -18,6 +18,7 @@ use Rector\NodeTypeResolver\Node\Attribute;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\ConfiguredCodeSample;
use Rector\RectorDefinition\RectorDefinition;
use function Safe\sprintf;
final class MoveRepositoryFromParentToConstructorRector extends AbstractRector
{

View File

@ -19,6 +19,7 @@ use Rector\NodeTypeResolver\Node\Attribute;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
use function Safe\sprintf;
final class ServiceLocatorToDIRector extends AbstractRector
{

View File

@ -20,6 +20,7 @@ use Rector\Exception\NotImplementedException;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
use function Safe\sprintf;
final class UnnecessaryTernaryExpressionRector extends AbstractRector
{

View File

@ -13,6 +13,8 @@ use Rector\NodeTypeResolver\Node\Attribute;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\ConfiguredCodeSample;
use Rector\RectorDefinition\RectorDefinition;
use function Safe\krsort;
use function Safe\substr;
final class NamespaceReplacerRector extends AbstractRector
{

View File

@ -4,6 +4,7 @@ namespace Rector\RectorDefinition;
use Rector\Contract\RectorDefinition\CodeSampleInterface;
use Rector\Exception\RectorDefinition\CodeSamplesMissingException;
use function Safe\sprintf;
final class RectorDefinition
{

View File

@ -9,6 +9,7 @@ use Rector\DependencyInjection\ContainerFactory;
use Rector\FileSystem\FileGuard;
use Symfony\Component\Finder\SplFileInfo;
use Symplify\PackageBuilder\Parameter\ParameterProvider;
use function Safe\sprintf;
abstract class AbstractRectorTestCase extends TestCase
{
@ -71,10 +72,11 @@ abstract class AbstractRectorTestCase extends TestCase
$splFileInfo = new SplFileInfo($file, '', '');
$reconstructedFileContent = $this->fileProcessor->processFileToString($splFileInfo);
$this->assertStringEqualsFile($reconstructedFile, $reconstructedFileContent, sprintf(
'Original file "%s" did not match the result.',
$file
));
$this->assertStringEqualsFile(
$reconstructedFile,
$reconstructedFileContent,
sprintf('Original file "%s" did not match the result.', $file)
);
}
abstract protected function provideConfig(): string;