Skip to content

Commit

Permalink
Merge pull request #556 from VincentLanglet/php8
Browse files Browse the repository at this point in the history
Php8
  • Loading branch information
goetas authored Jan 12, 2021
2 parents de63a3e + f69c6dd commit a83dfa8
Show file tree
Hide file tree
Showing 15 changed files with 76 additions and 41 deletions.
17 changes: 11 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ php:
- 7.2
- 7.3
- 7.4
- 8.0

env:
global:
Expand All @@ -30,17 +31,21 @@ matrix:
- php: 7.4
env: SYMFONY_VERSION=^3.4
- php: 7.2
env: SYMFONY_VERSION=^4.3
env: SYMFONY_VERSION=^4.4
- php: 7.3
env: SYMFONY_VERSION=^4.3
env: SYMFONY_VERSION=^4.4
- php: 7.4
env: SYMFONY_VERSION=^4.3
env: SYMFONY_VERSION=^4.4
- php: 8.0
env: SYMFONY_VERSION=^4.4
- php: 7.2
env: SYMFONY_VERSION=^5.0
env: SYMFONY_VERSION=^5.2
- php: 7.3
env: SYMFONY_VERSION=^5.0
env: SYMFONY_VERSION=^5.2
- php: 7.4
env: SYMFONY_VERSION=^5.0
env: SYMFONY_VERSION=^5.2
- php: 8.0
env: SYMFONY_VERSION=^5.2

install:
- if [ -x .travis/install_${TARGET}.sh ]; then .travis/install_${TARGET}.sh; fi;
Expand Down
8 changes: 5 additions & 3 deletions Command/ResourcesListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$files = $this->retrieveFiles($dirs);

foreach ($files as $file) {
$path = str_replace($basePath, '%kernel.project_dir%', $file);
$path = str_replace($basePath, '%kernel.project_dir%', (string) $file);
$output->writeln(sprintf(' - %s', $path));
}

Expand Down Expand Up @@ -145,8 +145,10 @@ private function retrieveDirs()
}

// TODO: Remove this block when dropping support of Symfony 4
if ($this->rootDir !== null &&
is_dir($dir = $this->rootDir . '/Resources/translations')) {
if (
$this->rootDir !== null &&
is_dir($dir = $this->rootDir . '/Resources/translations')
) {
$dirs[] = $dir;
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/Translation/Extractor/FileExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private function extract($directory)
$twig = new Environment(new ArrayLoader([]));
$twig->addExtension(new SymfonyTranslationExtension($translator = new IdentityTranslator()));
$twig->addExtension(new TranslationExtension($translator));
$loader =new FilesystemLoader(realpath(__DIR__ . '/Fixture/SimpleTest/Resources/views/'));
$loader = new FilesystemLoader(realpath(__DIR__ . '/Fixture/SimpleTest/Resources/views/'));
$twig->setLoader($loader);

$docParser = new DocParser();
Expand Down
4 changes: 2 additions & 2 deletions Translation/Dumper/ArrayStructureDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public function dump(MessageCatalogue $catalogue, $domain = 'messages')
// are before sub-paths, e.g.
// array_keys($structure) = array('foo.bar', 'foo.bar.baz')
// but NOT: array_keys($structure) = array('foo.bar.baz', 'foo.bar')
for ($i=0, $c=count($parts); $i<$c; $i++) {
if ($i+1 === $c) {
for ($i = 0, $c = count($parts); $i < $c; $i++) {
if ($i + 1 === $c) {
$pointer[$parts[$i]] = $message;
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,10 @@ public function enterNode(Node $node)
}
$ref = new \ReflectionClass($name);

if (!$ref->isSubclassOf(AuthenticationException::class)
&& $ref->name !== 'Symfony\Component\Security\Core\Exception\AuthenticationException') {
if (
!$ref->isSubclassOf(AuthenticationException::class)
&& $ref->name !== 'Symfony\Component\Security\Core\Exception\AuthenticationException'
) {
return;
}

Expand Down
6 changes: 4 additions & 2 deletions Translation/Extractor/File/DefaultPhpFileExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,10 @@ public function enterNode(Node $node)
if ($node instanceof Node\Expr\MethodCall) {
$methodCallNodeName = $node->name instanceof Node\Identifier ? $node->name->name : $node->name;
}
if (!is_string($methodCallNodeName)
|| !in_array(strtolower($methodCallNodeName), array_map('strtolower', array_keys($this->methodsToExtractFrom)))) {
if (
!is_string($methodCallNodeName)
|| !in_array(strtolower($methodCallNodeName), array_map('strtolower', array_keys($this->methodsToExtractFrom)))
) {
$this->previousNode = $node;

return;
Expand Down
6 changes: 4 additions & 2 deletions Translation/Extractor/File/FormExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,8 @@ protected function parseConstraintNode(Node $item, $domain)
}

foreach ($item->value->items as $subItem) {
if (!$subItem->value instanceof Node\Expr\New_
if (
!$subItem->value instanceof Node\Expr\New_
|| !$subItem->value->args
|| !property_exists($subItem->value->args[0]->value, 'items')
) {
Expand Down Expand Up @@ -355,7 +356,8 @@ private function parseDefaultsCall(Node $node)
return;
}

if (isset($node->args[1])
if (
isset($node->args[1])
&& $node->args[0]->value instanceof Node\Scalar\String_
&& $node->args[1]->value instanceof Node\Scalar\String_
&& 'translation_domain' === $node->args[0]->value->value
Expand Down
2 changes: 1 addition & 1 deletion Translation/Extractor/File/TwigFileExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ protected function doEnterNode(Node $node, Environment $env)
$message = new Message($id, $domain);
$message->addSource($this->fileSourceFactory->create($this->file, $node->getTemplateLine()));

for ($i=count($this->stack)-2; $i>=0; $i-=1) {
for ($i = count($this->stack) - 2; $i >= 0; $i -= 1) {
if (!$this->stack[$i] instanceof FilterExpression) {
break;
}
Expand Down
8 changes: 5 additions & 3 deletions Translation/Extractor/File/ValidationExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,13 @@ class ValidationExtractor implements FileVisitorInterface, NodeVisitor

public function __construct($metadataFactory)
{
if (! (
if (
! (
$metadataFactory instanceof MetadataFactoryInterface
|| $metadataFactory instanceof LegacyMetadataFactoryInterface
|| $metadataFactory instanceof ClassMetadataFactoryInterface
)) {
)
) {
throw new \InvalidArgumentException(sprintf('%s expects an instance of MetadataFactoryInterface or ClassMetadataFactoryInterface', static::class));
}
$this->metadataFactory = $metadataFactory;
Expand Down Expand Up @@ -98,7 +100,7 @@ public function enterNode(Node $node)
return;
}

$metadata = $this->metadataFactory instanceof ClassMetadataFactoryInterface? $this->metadataFactory->getClassMetadata($name) : $this->metadataFactory->getMetadataFor($name);
$metadata = $this->metadataFactory instanceof ClassMetadataFactoryInterface ? $this->metadataFactory->getClassMetadata($name) : $this->metadataFactory->getMetadataFor($name);
if (!$metadata->hasConstraints() && !count($metadata->getConstrainedProperties())) {
return;
}
Expand Down
20 changes: 16 additions & 4 deletions Translation/Loader/XliffLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ class XliffLoader implements LoaderInterface
public function load($resource, $locale, $domain = 'messages')
{
$previousErrors = libxml_use_internal_errors(true);
$previousEntities = libxml_disable_entity_loader(false);
$previousEntities = $this->libxmlDisableEntityLoader(false);
if (false === $doc = simplexml_load_file((string) $resource)) {
libxml_use_internal_errors($previousErrors);
libxml_disable_entity_loader($previousEntities);
$this->libxmlDisableEntityLoader($previousEntities);
$libxmlError = libxml_get_last_error();

throw new RuntimeException(sprintf('Could not load XML-file "%s": %s', $resource, $libxmlError->message));
}

libxml_use_internal_errors($previousErrors);
libxml_disable_entity_loader($previousEntities);
$this->libxmlDisableEntityLoader($previousEntities);

$doc->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:1.2');
$doc->registerXPathNamespace('jms', 'urn:jms:translation');
Expand All @@ -67,7 +67,7 @@ public function load($resource, $locale, $domain = 'messages')
->setLocaleString((string) $trans->target);
\assert($m instanceof Message);

$m->setApproved((string) $trans['approved']==='yes');
$m->setApproved((string) $trans['approved'] === 'yes');

if (isset($trans->target['state'])) {
$m->setState((string) $trans->target['state']);
Expand Down Expand Up @@ -119,4 +119,16 @@ public function load($resource, $locale, $domain = 'messages')

return $catalogue;
}

/**
* Use libxml_disable_entity_loader only if it's not deprecated
*/
private function libxmlDisableEntityLoader(bool $disable): bool
{
if (PHP_VERSION_ID >= 80000) {
return true;
}

return libxml_disable_entity_loader($disable);
}
}
2 changes: 1 addition & 1 deletion Translation/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ private function detectOutputFormat($currentDomain)
$otherDomainFormat = $localeFormat = $otherLocaleFormat = null;
foreach (FileUtils::findTranslationFiles($this->config->getTranslationsDir()) as $domain => $locales) {
foreach ($locales as $locale => $fileData) {
[$format ] = $fileData;
[$format] = $fileData;

if ($currentDomain !== $domain) {
$otherDomainFormat = $format;
Expand Down
12 changes: 8 additions & 4 deletions Twig/DefaultApplyingNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,16 @@ public function doEnterNode(Node $node, Environment $env)
return $node;
}

if ($node instanceof FilterExpression
&& 'desc' === $node->getNode('filter')->getAttribute('value')) {
if (
$node instanceof FilterExpression
&& 'desc' === $node->getNode('filter')->getAttribute('value')
) {
$transNode = $node->getNode('node');
while ($transNode instanceof FilterExpression
while (
$transNode instanceof FilterExpression
&& 'trans' !== $transNode->getNode('filter')->getAttribute('value')
&& 'transchoice' !== $transNode->getNode('filter')->getAttribute('value')) {
&& 'transchoice' !== $transNode->getNode('filter')->getAttribute('value')
) {
$transNode = $transNode->getNode('node');
}

Expand Down
6 changes: 4 additions & 2 deletions Twig/NormalizingNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ protected function doEnterNode(Node $node, Environment $env)
*/
protected function doLeaveNode(Node $node, Environment $env)
{
if ($node instanceof ConcatBinary
if (
$node instanceof ConcatBinary
&& ($left = $node->getNode('left')) instanceof ConstantExpression
&& ($right = $node->getNode('right')) instanceof ConstantExpression) {
&& ($right = $node->getNode('right')) instanceof ConstantExpression
) {
return new ConstantExpression($left->getAttribute('value') . $right->getAttribute('value'), $left->getTemplateLine());
}

Expand Down
12 changes: 7 additions & 5 deletions Util/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function writeln($content)
public function revert()
{
$change = array_pop($this->changes);
$this->changeCount -=1;
$this->changeCount -= 1;
$this->content = substr($this->content, 0, -1 * strlen($change));
}

Expand All @@ -110,16 +110,18 @@ public function write($content)
$addition = '';

$lines = explode("\n", $content);
for ($i=0, $c=count($lines); $i<$c; $i++) {
if ($this->indentationLevel > 0
for ($i = 0, $c = count($lines); $i < $c; $i++) {
if (
$this->indentationLevel > 0
&& !empty($lines[$i])
&& ((empty($addition) && "\n" === substr($this->content, -1)) || "\n" === substr($addition, -1))) {
&& ((empty($addition) && "\n" === substr($this->content, -1)) || "\n" === substr($addition, -1))
) {
$addition .= str_repeat(' ', $this->indentationLevel * $this->indentationSpaces);
}

$addition .= $lines[$i];

if ($i+1 < $c) {
if ($i + 1 < $c) {
$addition .= "\n";
}
}
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
}
],
"require": {
"php": "^7.2",
"php": "^7.2 || ^8.0",
"nikic/php-parser": "^1.4 || ^2.0 || ^3.0 || ^4.0",
"symfony/console": "^3.4 || ^4.3 || ^5.0",
"symfony/framework-bundle": "^3.4.31 || ^4.3 || ^5.0",
Expand All @@ -32,7 +32,7 @@
},
"require-dev": {
"doctrine/annotations": "^1.8",
"doctrine/coding-standard": "^7.0",
"doctrine/coding-standard": "^8.0",
"matthiasnoback/symfony-dependency-injection-test": "^4.1",
"nyholm/nsa": "^1.0.1",
"phpunit/phpunit": "^8.3",
Expand All @@ -42,7 +42,7 @@
"symfony/browser-kit": "^3.4 || ^4.3 || ^5.0",
"symfony/css-selector": "^3.4 || ^4.3 || ^5.0",
"symfony/expression-language": "^3.4 || ^4.3 || ^5.0",
"symfony/filesystem": "^5.1",
"symfony/filesystem": "^3.4 || ^4.3 || ^5.0",
"symfony/form": "^3.4 || ^4.3 || ^5.0",
"symfony/security-csrf": "^3.4 || ^4.3 || ^5.0",
"symfony/templating": "^3.4 || ^4.3 || ^5.0",
Expand Down

0 comments on commit a83dfa8

Please sign in to comment.