Skip to content

Commit

Permalink
Merge pull request #2814 from briannesbitt/3.x-merge
Browse files Browse the repository at this point in the history
3.x merge
  • Loading branch information
kylekatarnls authored Jun 28, 2023
2 parents e44b7fa + 74ba1ba commit daa6003
Show file tree
Hide file tree
Showing 14 changed files with 286 additions and 96 deletions.
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
"require": {
"php": "^8.1",
"ext-json": "*",
"psr/clock": "^1.0",
"symfony/clock": "^6.3",
"symfony/polyfill-mbstring": "^1.0",
"symfony/translation": "^4.4.18 || ^5.2.1|| ^6.0"
},
Expand All @@ -57,6 +59,9 @@
"phpunit/phpunit": "^10.2.2",
"squizlabs/php_codesniffer": "^3.7.2"
},
"provide": {
"psr/clock-implementation": "1.0"
},
"minimum-stability": "dev",
"prefer-stable": true,
"autoload": {
Expand Down
82 changes: 53 additions & 29 deletions phpdoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Carbon\CarbonInterface;
use Carbon\Factory;
use Carbon\FactoryImmutable;

$tags = [
'property',
Expand Down Expand Up @@ -108,7 +110,30 @@ function cleanClassName($name)
return preg_replace('/^\\\\(Date(?:Time(?:Immutable|Interface|Zone)?|Interval)|[a-z]*Exception|Closure)$/i', '$1', preg_replace('/^\\\\Carbon\\\\/', '', $name));
}

function dumpParameter($method, ReflectionParameter $parameter)
function dumpType(ReflectionType $type, bool $deep = true, bool $allowsNull = false): string
{
if ($type instanceof ReflectionUnionType) {
return ($deep ? '(' : '').implode('|', array_map(
dumpType(...),
$type->getTypes(),
)).($deep ? ')' : '');
}

if ($type instanceof ReflectionIntersectionType) {
return ($deep ? '(' : '').implode('&', array_map(
dumpType(...),
$type->getTypes(),
)).($deep ? ')' : '');
}

$name = cleanClassName($type->getName());

return (!$deep && $allowsNull ? '?' : '').
$name.
($deep && $allowsNull ? '|null' : '');
}

function dumpParameter(string $method, ReflectionParameter $parameter): string
{
global $defaultValues;

Expand All @@ -119,14 +144,8 @@ function dumpParameter($method, ReflectionParameter $parameter)
$output = "...$output";
}

if ($parameter->getType()) {
$name = cleanClassName($parameter->getType()->getName());

if ($parameter->allowsNull()) {
$name = "?$name";
}

$output = "$name $output";
if ($parameter->hasType()) {
$output = dumpType($parameter->getType(), false, $parameter->allowsNull())." $output";
}

if (isset($defaultValues[$method])) {
Expand All @@ -137,11 +156,8 @@ function dumpParameter($method, ReflectionParameter $parameter)
return $output;
}

try {
if ($parameter->isDefaultValueAvailable()) {
$output .= ' = '.dumpValue($parameter->getDefaultValue());
}
} catch (ReflectionException) {
if ($parameter->isDefaultValueAvailable()) {
$output .= ' = '.dumpValue($parameter->getDefaultValue());
}

return $output;
Expand Down Expand Up @@ -683,22 +699,30 @@ function getMethodReturnType(ReflectionMethod $method)
}

$returnType = str_replace('static|CarbonInterface', 'static', $returnType ?: 'static');
$staticMethods[] = [
'@method',
str_replace(['self', 'static'], 'Carbon', $returnType),
"$method($parameters)",
$doc[0],
];
$staticImmutableMethods[] = [
'@method',
str_replace(['self', 'static'], 'CarbonImmutable', $returnType),
"$method($parameters)",
$doc[0],
];
if (!method_exists(Factory::class, $method)) {
$staticMethods[] = [
'@method',
str_replace(['self', 'static'], 'Carbon', $returnType),
"$method($parameters)",
$doc[0],
];

for ($i = 1; $i < \count($doc); $i++) {
$staticMethods[] = ['', '', '', $doc[$i]];
}
}

for ($i = 1; $i < \count($doc); $i++) {
$staticMethods[] = ['', '', '', $doc[$i]];
$staticImmutableMethods[] = ['', '', '', $doc[$i]];
if (!method_exists(FactoryImmutable::class, $method)) {
$staticImmutableMethods[] = [
'@method',
str_replace(['self', 'static'], 'CarbonImmutable', $returnType),
"$method($parameters)",
$doc[0],
];

for ($i = 1; $i < \count($doc); $i++) {
$staticImmutableMethods[] = ['', '', '', $doc[$i]];
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Carbon/CarbonImmutable.php
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ class CarbonImmutable extends DateTimeImmutable implements CarbonInterface
__clone as dateTraitClone;
}

public function __clone()
public function __clone(): void
{
$this->dateTraitClone();
$this->endOfTime = false;
Expand Down
17 changes: 6 additions & 11 deletions src/Carbon/CarbonInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -667,20 +667,17 @@ public static function __callStatic($method, $parameters);
/**
* Update constructedObjectId on cloned.
*/
public function __clone();
public function __clone(): void;

/**
* Create a new Carbon instance.
*
* Please see the testing aids section (specifically static::setTestNow())
* for more on the possibility of this constructor returning a test instance.
*
* @param DateTimeInterface|string|null $time
* @param DateTimeZone|string|null $tz
*
* @throws InvalidFormatException
*/
public function __construct($time = null, $tz = null);
public function __construct(DateTimeInterface|string|int|float|null $time = null, DateTimeZone|string|int|null $tz = null);

/**
* Show truthy properties on var_dump().
Expand Down Expand Up @@ -2379,12 +2376,8 @@ public static function hasTestNow();

/**
* Create a Carbon instance from a DateTime one.
*
* @param DateTimeInterface $date
*
* @return static
*/
public static function instance($date): static;
public static function instance(DateTimeInterface $date): static;

/**
* Returns true if the current date matches the given string.
Expand Down Expand Up @@ -3289,7 +3282,7 @@ public function notEqualTo($date): bool;
*
* @return static
*/
public static function now($tz = null);
public static function now(DateTimeZone|string|int|null $tz = null): static;

/**
* Returns a present instance in the same timezone.
Expand Down Expand Up @@ -3961,6 +3954,8 @@ public function since($other = null, $syntax = null, $short = false, $parts = 1,
*/
public static function singularUnit(string $unit): string;

public static function sleep(int|float $seconds): void;

/**
* Modify to start of current given unit.
*
Expand Down
16 changes: 9 additions & 7 deletions src/Carbon/CarbonTimeZone.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ public function cast(string $className)
/**
* Create a CarbonTimeZone from mixed input.
*
* @param DateTimeZone|string|int|null $object original value to get CarbonTimeZone from it.
* @param DateTimeZone|string|int|null $objectDump dump of the object for error messages.
* @param DateTimeZone|string|int|false|null $object original value to get CarbonTimeZone from it.
* @param DateTimeZone|string|int|false|null $objectDump dump of the object for error messages.
*
* @throws InvalidTimeZoneException
*
Expand All @@ -102,12 +102,14 @@ public static function instance($object = null, $objectDump = null): ?self
return new static();
}

if (!$tz instanceof DateTimeZone) {
$tz = static::getDateTimeZoneFromName($object);
}
if ($tz !== false) {
if (!$tz instanceof DateTimeZone) {
$tz = static::getDateTimeZoneFromName($object);
}

if ($tz !== null) {
return new static($tz->getName());
if ($tz !== null) {
return new static($tz->getName());
}
}

if (Carbon::isStrictModeEnabled()) {
Expand Down
5 changes: 3 additions & 2 deletions src/Carbon/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
* @method bool hasRelativeKeywords($time) Determine if a time string will produce a relative date.
* @method bool hasTestNow() Determine if there is a valid test instance set. A valid test instance
* is anything that is not null.
* @method Carbon instance($date) Create a Carbon instance from a DateTime one.
* @method Carbon instance(DateTimeInterface $date) Create a Carbon instance from a DateTime one.
* @method bool isImmutable() Returns true if the current class/instance is immutable.
* @method bool isModifiableUnit($unit) Returns true if a property can be changed via setter.
* @method bool isMutable() Returns true if the current class/instance is mutable.
Expand All @@ -112,7 +112,7 @@
* Always return a new instance. Parse only strings and only these likely to be dates (skip intervals
* and recurrences). Throw an exception for invalid format, but otherwise return null.
* @method void mixin($mixin) Mix another object into the class.
* @method Carbon now($tz = null) Get a Carbon instance for the current date and time.
* @method Carbon now(DateTimeZone|string|int|null $tz = null) Get a Carbon instance for the current date and time.
* @method Carbon parse($time = null, $tz = null) Create a carbon instance from a string.
* This is an alias for the constructor that allows better fluent syntax
* as it allows you to do Carbon::parse('Monday next week')->fn() rather
Expand Down Expand Up @@ -206,6 +206,7 @@
* @method bool shouldOverflowMonths() Get the month overflow global behavior (can be overridden in specific instances).
* @method bool shouldOverflowYears() Get the month overflow global behavior (can be overridden in specific instances).
* @method string singularUnit(string $unit) Returns standardized singular of a given singular/plural unit name (in English).
* @method void sleep(int|float $seconds)
* @method Carbon today($tz = null) Create a Carbon instance for today.
* @method Carbon tomorrow($tz = null) Create a Carbon instance for tomorrow.
* @method string translateTimeString(string $timeString, ?string $from = null, ?string $to = null, int $mode = CarbonInterface::TRANSLATE_ALL) Translate a time string from a locale to an other.
Expand Down
41 changes: 38 additions & 3 deletions src/Carbon/FactoryImmutable.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
namespace Carbon;

use Closure;
use DateTimeZone;
use Symfony\Component\Clock\ClockInterface;
use Symfony\Component\Clock\NativeClock;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
Expand Down Expand Up @@ -89,7 +92,7 @@
* @method bool hasRelativeKeywords($time) Determine if a time string will produce a relative date.
* @method bool hasTestNow() Determine if there is a valid test instance set. A valid test instance
* is anything that is not null.
* @method CarbonImmutable instance($date) Create a Carbon instance from a DateTime one.
* @method CarbonImmutable instance(DateTimeInterface $date) Create a Carbon instance from a DateTime one.
* @method bool isImmutable() Returns true if the current class/instance is immutable.
* @method bool isModifiableUnit($unit) Returns true if a property can be changed via setter.
* @method bool isMutable() Returns true if the current class/instance is mutable.
Expand All @@ -110,7 +113,6 @@
* Always return a new instance. Parse only strings and only these likely to be dates (skip intervals
* and recurrences). Throw an exception for invalid format, but otherwise return null.
* @method void mixin($mixin) Mix another object into the class.
* @method CarbonImmutable now($tz = null) Get a Carbon instance for the current date and time.
* @method CarbonImmutable parse($time = null, $tz = null) Create a carbon instance from a string.
* This is an alias for the constructor that allows better fluent syntax
* as it allows you to do Carbon::parse('Monday next week')->fn() rather
Expand Down Expand Up @@ -226,7 +228,40 @@
*
* </autodoc>
*/
class FactoryImmutable extends Factory
class FactoryImmutable extends Factory implements ClockInterface
{
protected $className = CarbonImmutable::class;

protected DateTimeZone|string|int|null $defaultTimezone = null;

/**
* Get a Carbon instance for the current date and time.
*/
public function now(DateTimeZone|string|int|null $tz = null): CarbonImmutable
{
$className = $this->className;

return new $className(null, $tz ?? $this->defaultTimezone);
}

public function sleep(int|float $seconds): void
{
$className = $this->className;

if ($className::hasTestNow()) {
$className::setTestNow($className::getTestNow()->avoidMutation()->addSeconds($seconds));

return;
}

(new NativeClock('UTC'))->sleep($seconds);
}

public function withTimeZone(DateTimeZone|string|int|null $timezone): static
{
$factory = new static();
$factory->defaultTimezone = $timezone;

return $factory;
}
}
Loading

0 comments on commit daa6003

Please sign in to comment.