Skip to content

Commit

Permalink
Merge pull request #2518 from briannesbitt/merge-3.x-master
Browse files Browse the repository at this point in the history
Merge 3.x master
  • Loading branch information
kylekatarnls committed Dec 4, 2021
2 parents ac1556c + e661141 commit 33ccae4
Show file tree
Hide file tree
Showing 19 changed files with 386 additions and 260 deletions.
13 changes: 11 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ jobs:
include:
- php: 8.0
coverage: true

- php: 8.0
classmap-authoritative: true

- php: 8.0
- php: 8.0
setup: lowest

Expand All @@ -37,7 +42,11 @@ jobs:
- php: 8.0
laravel: true

name: ${{ matrix.laravel && 'Laravel compatibility - ' || '' }}${{ matrix.coverage && 'Coverage - ' || '' }}PHP ${{ matrix.php }} - ${{ matrix.setup || 'stable' }} - ubuntu
- php: 8.0
laravel: true
classmap-authoritative: true

name: ${{ matrix.laravel && 'Laravel compatibility - ' || '' }}${{ matrix.classmap-authoritative && 'classmap-authoritative - ' || '' }}${{ matrix.coverage && 'Coverage - ' || '' }}PHP ${{ matrix.php }} - ${{ matrix.setup || 'stable' }} - ubuntu

steps:
- uses: actions/checkout@v2
Expand Down Expand Up @@ -65,7 +74,7 @@ jobs:
if [[ "${{ matrix.laravel }}" != 'true' ]]; then
composer remove --no-update kylekatarnls/multi-tester --no-interaction;
fi;
composer update --prefer-dist --no-progress --prefer-${{ matrix.setup || 'stable' }} ${{ (matrix.php >= 8.1 || matrix.php < 8) && '--ignore-platform-req=php' || '' }};
composer update --prefer-dist --no-progress --prefer-${{ matrix.setup || 'stable' }} ${{ (matrix.php >= 8.1 || matrix.php < 8) && '--ignore-platform-req=php' || '' }} ${{ matrix.classmap-authoritative && '--classmap-authoritative' || '' }};
- name: Run test suite
run: |
Expand Down
2 changes: 1 addition & 1 deletion build.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@

$zip->open($archive, ZipArchive::CREATE | ZipArchive::OVERWRITE);

foreach (['src', 'vendor', 'Carbon'] as $directory) {
foreach (['src', 'vendor', 'Carbon', 'lazy'] as $directory) {
if (is_dir($directory)) {
$directory = realpath($directory);
$base = \dirname($directory);
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"kylekatarnls/multi-tester": "^2.0",
"phpmd/phpmd": "^2.9",
"phpstan/extension-installer": "^1.0",
"phpstan/phpstan": "^0.12.54",
"phpstan/phpstan": "^0.12.54 || ^1.0",
"phpunit/phpunit": "^9.5.2",
"squizlabs/php_codesniffer": "^3.4"
},
Expand Down
43 changes: 43 additions & 0 deletions lazy/Carbon/PHPStan/MacroStrongType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

/**
* This file is part of the Carbon package.
*
* (c) Brian Nesbitt <brian@nesbot.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Carbon\PHPStan;

if (!class_exists(LazyMacro::class, false)) {
abstract class LazyMacro extends AbstractMacro
{
/**
* {@inheritdoc}
*/
public function getFileName(): ?string
{
return $this->reflectionFunction->getFileName();
}

/**
* {@inheritdoc}
*/
public function getStartLine(): ?int
{
return $this->reflectionFunction->getStartLine();
}

/**
* {@inheritdoc}
*/
public function getEndLine(): ?int
{
return $this->reflectionFunction->getEndLine();
}
}
}
49 changes: 49 additions & 0 deletions lazy/Carbon/PHPStan/MacroWeakType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

/**
* This file is part of the Carbon package.
*
* (c) Brian Nesbitt <brian@nesbot.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Carbon\PHPStan;

if (!class_exists(LazyMacro::class, false)) {
abstract class LazyMacro extends AbstractMacro
{
/**
* {@inheritdoc}
*
* @return string|false
*/
public function getFileName()
{
return $this->reflectionFunction->getFileName();
}

/**
* {@inheritdoc}
*
* @return int|false
*/
public function getStartLine()
{
return $this->reflectionFunction->getStartLine();
}

/**
* {@inheritdoc}
*
* @return int|false
*/
public function getEndLine()
{
return $this->reflectionFunction->getEndLine();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

use Symfony\Component\Translation\MessageCatalogueInterface;

if (!class_exists(Translator::class)) {
class Translator extends AbstractTranslator implements TranslatorStrongTypeInterface
if (!class_exists(LazyTranslator::class, false)) {
class LazyTranslator extends AbstractTranslator implements TranslatorStrongTypeInterface
{
public function trans(?string $id, array $parameters = [], ?string $domain = null, ?string $locale = null): string
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

namespace Carbon;

if (!class_exists(Translator::class)) {
class Translator extends AbstractTranslator
if (!class_exists(LazyTranslator::class, false)) {
class LazyTranslator extends AbstractTranslator
{
/**
* Returns the translation.
Expand Down
13 changes: 10 additions & 3 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,26 @@ parameters:
- '#^Call to an undefined method Carbon\\Carbon(Immutable)?::diffInReal([A-Za-z]+)\(\)\.$#'
- '#^Unsafe usage of new static\(\)\.#'
- '#^Method Carbon\\Carbon(Interface|Immutable)?::(add|sub)[A-Z][A-Za-z]+\(\) invoked with 1 parameter, 0 required\.#'
- '#^Access to an undefined property Carbon\\CarbonPeriod::\$locale\.$#'
# - '#^Access to an undefined property Carbon\\CarbonPeriod::\$locale\.$#'
- '#^Call to an undefined method Carbon\\Carbon(Interface|Immutable)?::(super|noThis)\(\)\.$#'
- '#Call to an undefined method DateTimeInterface::setTimezone\(\)\.#'
- '#should return (\S*)static\(Carbon\\CarbonInterval\)\|null but returns Carbon\\CarbonInterval(\|null)?\.$#'
- '#should return (\S*)\$this\(Carbon\\CarbonInterval\) but returns Carbon\\CarbonInterval\.$#'
- '#should return (\S*)static\(Carbon\\CarbonPeriod\) but returns Carbon\\CarbonPeriod\.$#'
# - '#should return (\S*)static\(Carbon\\CarbonPeriod\) but returns Carbon\\CarbonPeriod\.$#'
- '#should return (\S*)static\(Carbon\\Carbon\) but returns Carbon\\Carbon(Interface)?\.$#'
- '#should return (\S*)static\(Carbon\\CarbonImmutable\) but returns Carbon\\Carbon(Immutable|Interface)\.$#'
- '#should return (\S*)static\(Carbon\\CarbonImmutable\)\|null but returns Carbon\\Carbon(Immutable|Interface)\|null\.$#'
- '#^PHPDoc tag @mixin contains invalid type Carbon\\Traits\\DeprecatedProperties\.$#'
excludes_analyse:
- '#^Implementing PHPStan\\Reflection\\Php\\BuiltinMethodReflection is not covered by backward compatibility promise\.#'
- '#^Calling PHPStan\\Reflection\\Php\\PhpMethodReflectionFactory::create\(\) is not covered by backward compatibility promise\.#'
# - '#^Protected property Carbon\\CarbonPeriod::\$.+ overriding public property DatePeriod::\$.+ should also be public\.$#'
- '#^Method Carbon\\CarbonInterval::withOriginal\(\) should return T but returns \(Carbon\\CarbonInterval&T\)\|T\.$#'
- '#^Method Carbon\\CarbonInterval::castIntervalToClass\(\) should return T of DateInterval but returns \(Carbon\CarbonInterval&T of DateInterval\)|T of DateInterval\.$#'
excludePaths:
- '*/src/Carbon/CarbonPeriod.php'
- '*/src/Carbon/Laravel/ServiceProvider.php'
- '*/src/Carbon/TranslatorWeakType.php'
- '*/src/Carbon/PHPStan/Macro.php'
- '*/tests/Carbon/SerializationTest.php'
- '*/tests/CarbonImmutable/SetStateTest.php'
- '*/tests/CarbonImmutable/SerializationTest.php'
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<directory suffix=".php">src/Carbon</directory>
</include>
<exclude>
<file>src/Carbon/PHPStan/Macro.php</file>
<file>src/Carbon/Translator.php</file>
<file>src/Carbon/TranslatorStrongType.php</file>
<file>src/Carbon/TranslatorWeakType.php</file>
Expand Down
12 changes: 7 additions & 5 deletions src/Carbon/AbstractTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
use ReflectionException;
use ReflectionFunction;
use Symfony\Component\Translation;
use Symfony\Component\Translation\Formatter\MessageFormatterInterface;
use Symfony\Component\Translation\Loader\ArrayLoader;

class AbstractTranslator extends Translation\Translator
abstract class AbstractTranslator extends Translation\Translator
{
public const REGION_CODE_LENGTH = 2;

Expand Down Expand Up @@ -70,18 +72,18 @@ public static function get($locale = null)
$locale = $locale ?: 'en';

if (!isset(static::$singletons[$locale])) {
static::$singletons[$locale] = new static($locale ?: 'en');
static::$singletons[$locale] = new static($locale);
}

return static::$singletons[$locale];
}

public function __construct($locale, Translation\Formatter\MessageFormatterInterface $formatter = null, $cacheDir = null, $debug = false)
public function __construct($locale, MessageFormatterInterface $formatter = null, $cacheDir = null, $debug = false)
{
parent::setLocale($locale);
$this->initializing = true;
$this->directories = [__DIR__.'/Lang'];
$this->addLoader('array', new Translation\Loader\ArrayLoader());
$this->addLoader('array', new ArrayLoader());
parent::__construct($locale, $formatter, $cacheDir, $debug);
$this->initializing = false;
}
Expand Down Expand Up @@ -337,7 +339,7 @@ public function setLocale($locale)
$completeLocaleChunks = preg_split('/[_.-]+/', $completeLocale);

$getScore = function ($language) use ($completeLocaleChunks) {
return static::compareChunkLists($completeLocaleChunks, preg_split('/[_.-]+/', $language));
return self::compareChunkLists($completeLocaleChunks, preg_split('/[_.-]+/', $language));
};

usort($locales, function ($first, $second) use ($getScore) {
Expand Down
14 changes: 7 additions & 7 deletions src/Carbon/CarbonInterval.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ public function __construct($years = null, $months = null, $weeks = null, $days
if ($years instanceof DateInterval) {
parent::__construct(static::getDateIntervalSpec($years));
$this->f = $years->f;
static::copyNegativeUnits($years, $this);
self::copyNegativeUnits($years, $this);

return;
}
Expand Down Expand Up @@ -461,7 +461,7 @@ public static function getFactor($source, $target)
{
$source = self::standardizeUnit($source);
$target = self::standardizeUnit($target);
$factors = static::getFlipCascadeFactors();
$factors = self::getFlipCascadeFactors();

if (isset($factors[$source])) {
[$to, $factor] = $factors[$source];
Expand Down Expand Up @@ -2351,7 +2351,7 @@ public function total(string $unit): float
$result = 0;
$cumulativeFactor = 0;
$unitFound = false;
$factors = static::getFlipCascadeFactors();
$factors = self::getFlipCascadeFactors();
$daysPerWeek = static::getDaysPerWeek();

$values = [
Expand Down Expand Up @@ -2828,7 +2828,7 @@ private static function getFlipCascadeFactors(): array
if (!self::$flipCascadeFactors) {
self::$flipCascadeFactors = [];

foreach (static::getCascadeFactors() as $to => [$factor, $from]) {
foreach (self::getCascadeFactors() as $to => [$factor, $from]) {
self::$flipCascadeFactors[self::standardizeUnit($from)] = [self::standardizeUnit($to), $factor];
}
}
Expand Down Expand Up @@ -2864,10 +2864,10 @@ private static function castIntervalToClass(DateInterval $interval, string $clas
}

if ($interval instanceof self && is_a($className, self::class, true)) {
static::copyStep($interval, $instance);
self::copyStep($interval, $instance);
}

static::copyNegativeUnits($interval, $instance);
self::copyNegativeUnits($interval, $instance);

return self::withOriginal($instance, $interval);
}
Expand Down Expand Up @@ -2904,7 +2904,7 @@ private function doCascade(bool $deep): self
unset($originalData['days']);
$newData = $originalData;

foreach (static::getFlipCascadeFactors() as $source => [$target, $factor]) {
foreach (self::getFlipCascadeFactors() as $source => [$target, $factor]) {
foreach (['source', 'target'] as $key) {
if ($$key === 'dayz') {
$$key = 'daysExcludeWeeks';
Expand Down
12 changes: 9 additions & 3 deletions src/Carbon/CarbonPeriod.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,17 +176,23 @@ class CarbonPeriod extends DatePeriod implements Countable, JsonSerializable
use Options;

/**
* Built-in filters.
* Built-in filter for limit by recurrences.
*
* @var string
* @var callable
*/
public const RECURRENCES_FILTER = [self::class, 'filterRecurrences'];

/**
* Built-in filter for limit to an end.
*
* @var callable
*/
public const END_DATE_FILTER = [self::class, 'filterEndDate'];

/**
* Special value which can be returned by filters to end iteration. Also a filter.
*
* @var string
* @var callable
*/
public const END_ITERATION = [self::class, 'endIteration'];

Expand Down
Loading

0 comments on commit 33ccae4

Please sign in to comment.