diff --git a/src/Converter/Conversion.php b/src/Converter/Conversion.php index 2ea8a23..a10fa09 100644 --- a/src/Converter/Conversion.php +++ b/src/Converter/Conversion.php @@ -76,7 +76,7 @@ public function getTo() * * @param DateRepresentationInterface $to the to * - * @return self|null + * @return self */ public function setTo(DateRepresentationInterface $to) { @@ -92,7 +92,7 @@ public function setTo(DateRepresentationInterface $to) */ public function getUnixTime() { - return $this->unixTime; + return (int)$this->unixTime; } /** diff --git a/src/Converter/UnixTimeConverter/Date.php b/src/Converter/UnixTimeConverter/Date.php index 9248007..4b65ac2 100644 --- a/src/Converter/UnixTimeConverter/Date.php +++ b/src/Converter/UnixTimeConverter/Date.php @@ -24,13 +24,9 @@ class Date implements UnixTimeConverterInterface */ public function fromUnixTime(Conversion $conversion) { - if (null === $res = $conversion->getTo()) { - return; - } - $from = $conversion->getFrom(); - $res = $res + $res = $conversion->getTo() ->withUnixTime($from->getUnixTime()) ->withUnixMicroTime($from->getUnixMicroTime()) ->withTimezone($from->getTimezone()) diff --git a/src/Converter/UnixTimeConverter/DateSolar.php b/src/Converter/UnixTimeConverter/DateSolar.php index a2c14d4..285ca50 100644 --- a/src/Converter/UnixTimeConverter/DateSolar.php +++ b/src/Converter/UnixTimeConverter/DateSolar.php @@ -2,7 +2,6 @@ namespace Popy\Calendar\Converter\UnixTimeConverter; -use BCMathExtended\BC; use Popy\Calendar\Converter\Conversion; use Popy\Calendar\Converter\UnixTimeConverterInterface; use Popy\Calendar\Converter\CompleteLeapYearCalculatorInterface; @@ -68,7 +67,7 @@ public function fromUnixTime(Conversion $conversion) // TODO : remove code repetition by using a separate abstraction // handling basic math operations. - if (is_integer($this->dayLengthInSeconds) || !class_exists(BC::class)) { + if (is_integer($this->dayLengthInSeconds)) { // bc not needed/not available // Relative time from era start. @@ -95,12 +94,12 @@ public function fromUnixTime(Conversion $conversion) // Calculating global day index. Floor is used to properly handle // negative values - $eraDayIndex = BC::floor( + $eraDayIndex = floor( bcdiv($relativeTime, $this->dayLengthInSeconds) ); // TODO : handle the loss as microseconds ? - $time = BC::ceil(bcsub( + $time = ceil(bcsub( $relativeTime, bcmul($eraDayIndex, $this->dayLengthInSeconds) )); @@ -131,7 +130,7 @@ public function toUnixTime(Conversion $conversion) return; } - $year = $input->getYear(); + $year = (int)$input->getYear(); $eraDayIndex = $input->getDayIndex() + $this->calculator->getYearEraDayIndex($year) ; diff --git a/src/Converter/UnixTimeConverter/Iso8601Weeks.php b/src/Converter/UnixTimeConverter/Iso8601Weeks.php index c5f09f5..87e693f 100644 --- a/src/Converter/UnixTimeConverter/Iso8601Weeks.php +++ b/src/Converter/UnixTimeConverter/Iso8601Weeks.php @@ -56,7 +56,7 @@ public function fromUnixTime(Conversion $conversion) return; } - $year = $input->getYear(); + $year = (int)$input->getYear(); // Assuming the era starting year is 1970, it starts a Thursday. $dayOfWeek = ($input->getEraDayIndex() + $this->firstYearDayIndex) % 7; diff --git a/src/Converter/UnixTimeConverter/TimeOffset.php b/src/Converter/UnixTimeConverter/TimeOffset.php index f063032..7e83313 100644 --- a/src/Converter/UnixTimeConverter/TimeOffset.php +++ b/src/Converter/UnixTimeConverter/TimeOffset.php @@ -32,11 +32,9 @@ public function fromUnixTime(Conversion $conversion) $conversion->getUnixTime() + $offset->getValue() ); - if (null !== $conversion->getTo()) { - $conversion->setTo( - $conversion->getTo()->withOffset($offset) - ); - } + $conversion->setTo( + $conversion->getTo()->withOffset($offset) + ); } /** diff --git a/src/Factory/ConfigurableFactory.php b/src/Factory/ConfigurableFactory.php index a85473a..a71aeb9 100644 --- a/src/Factory/ConfigurableFactory.php +++ b/src/Factory/ConfigurableFactory.php @@ -82,7 +82,7 @@ class ConfigurableFactory /** * Available values for option "additional_symbol_parser" * - * @var array + * @var array */ protected $additional_symbol_parser = [ 'none' => false, @@ -135,8 +135,8 @@ public function buildConverter(array $options = array()) /** * Generic service getter. * - * @param string $service Service name. - * @param array &$options Option array + * @param string $service Service name. + * @param array $options Option array * * @return mixed */ diff --git a/src/Formatter/AgnosticFormatter.php b/src/Formatter/AgnosticFormatter.php index b2ea6f2..3de0dc6 100644 --- a/src/Formatter/AgnosticFormatter.php +++ b/src/Formatter/AgnosticFormatter.php @@ -56,9 +56,7 @@ public function format(DateTimeInterface $input, $format) { $date = Date::buildFromDateTimeInterface($input); - if (null === $date = $this->converter->to($date)) { - return; - } + $date = $this->converter->to($date); return $this->formatDateRepresentation($date, $format); } diff --git a/src/Formatter/NumberConverter/RFC2550.php b/src/Formatter/NumberConverter/RFC2550.php index 9ced7d7..2b21aec 100644 --- a/src/Formatter/NumberConverter/RFC2550.php +++ b/src/Formatter/NumberConverter/RFC2550.php @@ -103,7 +103,7 @@ public function from($input) * * @param string $input * - * @return string + * @return string|null */ public function convertFromPositive($input) { @@ -117,7 +117,7 @@ public function convertFromPositive($input) $matches ) ) { - return; + return null; } if ($matches['alpha'] === '') { @@ -148,7 +148,7 @@ public function convertFromPositive($input) * * @param string $input * - * @return string + * @return string|null */ public function convertFromNegative($input) { diff --git a/src/Formatter/SymbolFormatter/StandardDateFragmented.php b/src/Formatter/SymbolFormatter/StandardDateFragmented.php index d0ed68f..acba1d1 100644 --- a/src/Formatter/SymbolFormatter/StandardDateFragmented.php +++ b/src/Formatter/SymbolFormatter/StandardDateFragmented.php @@ -44,12 +44,16 @@ public function formatSymbol(DateRepresentationInterface $input, FormatToken $to if ($token->is('F')) { // F A full textual representation of a month - return (string)$this->locale->getMonthName($input->getDateParts()->get(0)); + return (string)$this->locale->getMonthName( + $input->getDateParts()->get(0) + ); } if ($token->is('M')) { // M A short textual representation of a month, three letters - return (string)$this->locale->getMonthShortName($input->getDateParts()->get(0)); + return (string)$this->locale->getMonthShortName( + $input->getDateParts()->get(0) + ); } if ($token->is('m')) { @@ -79,17 +83,23 @@ public function formatSymbol(DateRepresentationInterface $input, FormatToken $to if ($token->is('S')) { // S English ordinal suffix for the day of the month, 2 characters - return (string)$this->locale->getNumberOrdinalSuffix($input->getDateParts()->get(1)); + return $this->locale->getNumberOrdinalSuffix( + (int)$input->getDateParts()->get(1) + ); } if ($token->is('l')) { // l (lowercase 'L') A full textual representation of the day of the week - return (string)$this->locale->getDayName($input->getDateParts()->getTransversal(2)); + return (string)$this->locale->getDayName( + $input->getDateParts()->getTransversal(2) + ); } if ($token->is('D')) { // D A textual representation of a day, three letters - return (string)$this->locale->getDayShortName($input->getDateParts()->getTransversal(2)); + return (string)$this->locale->getDayShortName( + $input->getDateParts()->getTransversal(2) + ); } if ($token->is('w')) { diff --git a/src/Formatter/SymbolFormatter/StandardDateSolar.php b/src/Formatter/SymbolFormatter/StandardDateSolar.php index 2b4d210..de6f905 100644 --- a/src/Formatter/SymbolFormatter/StandardDateSolar.php +++ b/src/Formatter/SymbolFormatter/StandardDateSolar.php @@ -41,7 +41,7 @@ public function formatSymbol(DateRepresentationInterface $input, FormatToken $to } if ($token->is('y')) { // y A two digit representation of a year - return $this->converter->to($input->getYear()); + return $this->converter->to($input->getYear() ?: 0); } if ($token->is('Y')) { diff --git a/src/Parser/AgnosticParser.php b/src/Parser/AgnosticParser.php index a6b863e..bdae84c 100644 --- a/src/Parser/AgnosticParser.php +++ b/src/Parser/AgnosticParser.php @@ -85,20 +85,18 @@ public function parse($input, $format, DateTimeZone $timezone = null) */ public function parseToDateRepresentation($input, $format, DateTimeZone $timezone = null) { - if (null === $lexer = $this->parser->parseFormat($format)) { - return; - } + $lexer = $this->parser->parseFormat($format); if (null === $parts = $lexer->tokenizeDate($input)) { - return; + return null; } $date = Date::buildFromTimezone($timezone); if (null === $date = $this->mapper->map($parts, $date)) { - return; + return null; } - return $date; + return $this->mapper->map($parts, $date); } } diff --git a/src/Parser/DateLexer/PregChoice.php b/src/Parser/DateLexer/PregChoice.php index 15982ed..1fc4f2d 100644 --- a/src/Parser/DateLexer/PregChoice.php +++ b/src/Parser/DateLexer/PregChoice.php @@ -65,25 +65,30 @@ public function hydrateResult(DateLexerResult $result, $match, $offset = 1) return $offset; } + if ( + null === $this->symbol // nothing to map + || $match[$offset][1] === -1 // didn't match + ) { + return $offset + 1; + } + // Did match - if ($match[$offset][1] !== -1) { - $found = array_search($match[$offset][0], $this->choices); - - // DO THE MAGIC - if ($found === false) { - $res = preg_grep( - '/^' . preg_quote($match[$offset][0], '/') . '$/i', - $this->choices - ); - - if (count($res)) { - reset($res); - $found = key($res); - } + $found = array_search($match[$offset][0], $this->choices); + + // DO THE MAGIC + if ($found === false) { + $res = preg_grep( + '/^' . preg_quote($match[$offset][0], '/') . '$/i', + $this->choices + ); + + if (count($res)) { + reset($res); + $found = key($res); } - - $result->set($this->symbol, $found); } + + $result->set($this->symbol, $found); return $offset + 1; } diff --git a/src/Parser/FormatParser/Basic.php b/src/Parser/FormatParser/Basic.php index 8b2247f..f7dffcd 100644 --- a/src/Parser/FormatParser/Basic.php +++ b/src/Parser/FormatParser/Basic.php @@ -3,11 +3,9 @@ namespace Popy\Calendar\Parser\FormatParser; use Popy\Calendar\Parser\FormatLexerInterface; -use Popy\Calendar\Parser\FormatLexer\MbString; use Popy\Calendar\Parser\SymbolParserInterface; use Popy\Calendar\Parser\FormatParserInterface; use Popy\Calendar\Parser\DateLexer\Collection; -use Popy\Calendar\Parser\SymbolParser\PregNative; use Popy\Calendar\Parser\DateLexer\NonSymbolMatcher; /** @@ -37,13 +35,13 @@ class Basic implements FormatParserInterface /** * Class constructor. * - * @param FormatLexerInterface|null $lexer Format lexer. - * @param SymbolParserInterface|null $symbolParser Symbol Parser + * @param FormatLexerInterface $lexer Format lexer. + * @param SymbolParserInterface $symbolParser Symbol Parser */ - public function __construct(FormatLexerInterface $lexer = null, SymbolParserInterface $symbolParser = null) + public function __construct(FormatLexerInterface $lexer, SymbolParserInterface $symbolParser) { - $this->lexer = $lexer ?: new MbString(); - $this->symbolParser = $symbolParser ?: new PregNative(); + $this->lexer = $lexer; + $this->symbolParser = $symbolParser; } diff --git a/src/Parser/FormatParser/PregExtendedNative.php b/src/Parser/FormatParser/PregExtendedNative.php index c135204..d7b2785 100644 --- a/src/Parser/FormatParser/PregExtendedNative.php +++ b/src/Parser/FormatParser/PregExtendedNative.php @@ -4,12 +4,10 @@ use Popy\Calendar\Parser\FormatToken; use Popy\Calendar\Parser\FormatLexerInterface; -use Popy\Calendar\Parser\FormatLexer\MbString; use Popy\Calendar\Parser\FormatParserInterface; use Popy\Calendar\Parser\SymbolParserInterface; use Popy\Calendar\Parser\DateLexer\PregSimple; use Popy\Calendar\Parser\DateLexer\PregCollection; -use Popy\Calendar\Parser\SymbolParser\PregNative; /** * Preg based implementation of the native DateTimeInterface format, with an @@ -39,13 +37,13 @@ class PregExtendedNative implements FormatParserInterface /** * Class constructor. * - * @param FormatLexerInterface|null $lexer Format lexer. - * @param SymbolParserInterface|null $symbolParser Symbol Parser + * @param FormatLexerInterface $lexer Format lexer. + * @param SymbolParserInterface $symbolParser Symbol Parser */ - public function __construct(FormatLexerInterface $lexer = null, SymbolParserInterface $symbolParser = null) + public function __construct(FormatLexerInterface $lexer, SymbolParserInterface $symbolParser) { - $this->lexer = $lexer ?: new MbString(); - $this->symbolParser = $symbolParser ?: new PregNative(); + $this->lexer = $lexer; + $this->symbolParser = $symbolParser; } /** diff --git a/src/Parser/FormatToken.php b/src/Parser/FormatToken.php index 16d4d7a..8404b93 100644 --- a/src/Parser/FormatToken.php +++ b/src/Parser/FormatToken.php @@ -48,7 +48,7 @@ public function __construct($value, $type) /** * Gets the token value. * - * @return string + * @return string|null */ public function getValue() { @@ -58,7 +58,7 @@ public function getValue() /** * Gets the token name (possibly aliased). * - * @return string + * @return string|null */ public function getName() { @@ -97,7 +97,7 @@ public function is($symbol) * or one of the symbol contained in the first argument if it is an array * * @param array|string $symbols - * @param string ...$symbol + * @param string ...$symbols * * @return boolean */ diff --git a/src/Parser/ResultMapper/StandardDate.php b/src/Parser/ResultMapper/StandardDate.php index 113f271..0e6ed68 100644 --- a/src/Parser/ResultMapper/StandardDate.php +++ b/src/Parser/ResultMapper/StandardDate.php @@ -90,13 +90,13 @@ protected function determineOffset(DateLexerResult $parts, TimeOffset $offset) * Determine date's timezone. If an offset has been found, Timezone has * no effect on the date parsing, but will have on the date display. * - * @param DateLexerResult $parts Date lexer results. - * @param integer|null $offset Date offset if it has been found. - * @param DateTimeZone $inputTz Default timezone if any. + * @param DateLexerResult $parts Date lexer results. + * @param TimeOffset $offset Date offset. + * @param DateTimeZone|null $inputTz Default timezone if any. * * @return DateTimeZone */ - protected function determineTimezone(DateLexerResult $parts, TimeOffset $offset, DateTimeZone $inputTz) + protected function determineTimezone(DateLexerResult $parts, TimeOffset $offset, DateTimeZone $inputTz = null) { // e Timezone identifier (added in PHP 5.1.0) Examples: UTC, GMT, Atlantic/Azores // O Difference to Greenwich time (GMT) in hours Example: +0200 diff --git a/src/Parser/ResultMapper/StandardDateFragmented.php b/src/Parser/ResultMapper/StandardDateFragmented.php index 8526327..890a5a3 100644 --- a/src/Parser/ResultMapper/StandardDateFragmented.php +++ b/src/Parser/ResultMapper/StandardDateFragmented.php @@ -112,7 +112,7 @@ protected function determineDayOfWeek(DateLexerResult $parts) protected function determineWeekIndex(DateLexerResult $parts) { if (null === $w = $parts->get('W')) { - return; + return null; } return intval($w) - 1; diff --git a/src/Parser/SymbolParser/PregNativeDateFragmented.php b/src/Parser/SymbolParser/PregNativeDateFragmented.php index 0eebd59..86870f3 100644 --- a/src/Parser/SymbolParser/PregNativeDateFragmented.php +++ b/src/Parser/SymbolParser/PregNativeDateFragmented.php @@ -147,7 +147,7 @@ protected function buildSuffixesLexer(FormatToken $token) $i = 0; $repetitions = 0; - while (null !== $label = $this->locale->getNumberOrdinalSuffix($i++)) { + while ($label = $this->locale->getNumberOrdinalSuffix($i++)) { if (!in_array($label, $choices)) { $choices[] = $label; $repetitions = 0; diff --git a/src/PresetParser.php b/src/PresetParser.php index 7fcf9c8..58eb25d 100644 --- a/src/PresetParser.php +++ b/src/PresetParser.php @@ -40,7 +40,7 @@ public function __construct(ParserInterface $parser, $format) * * @param string $input * - * @return DateTimeInterface + * @return DateTimeInterface|null */ public function parse($input) { diff --git a/src/ValueObject/AbstractFragmentedDuration.php b/src/ValueObject/AbstractFragmentedDuration.php index f2861ea..8cd7603 100644 --- a/src/ValueObject/AbstractFragmentedDuration.php +++ b/src/ValueObject/AbstractFragmentedDuration.php @@ -254,17 +254,18 @@ public function withTransversals(array $transversals) /** * Ensure each consecutive key of the input array is set (filling with nulls) * - * @param array $input + * @param array $input + * @param mixed $default Filling value. * - * @return array + * @return array */ - protected function fillArrayInput(array $input) + protected function fillArrayInput(array $input, $default = 0) { if (empty($input)) { return []; } - $res = array_fill(0, max(array_keys($input)), null); + $res = array_fill($default, max(array_keys($input)), null); foreach ($input as $index => $value) { $res[$index] = $value; diff --git a/src/ValueObject/DateRepresentation/Date.php b/src/ValueObject/DateRepresentation/Date.php index 4b32b0e..5eda25d 100644 --- a/src/ValueObject/DateRepresentation/Date.php +++ b/src/ValueObject/DateRepresentation/Date.php @@ -30,7 +30,7 @@ class Date implements DateRepresentationInterface /** * Timezone. * - * @var DateTimeZone + * @var DateTimeZone|null */ protected $timezone; diff --git a/src/ValueObject/Time.php b/src/ValueObject/Time.php index 3a3ee97..661d694 100644 --- a/src/ValueObject/Time.php +++ b/src/ValueObject/Time.php @@ -54,7 +54,7 @@ public function __construct(array $fragments = [], array $sizes = [], $ratio = n public function get($i) { if (!isset($this->fragments[$i])) { - return; + return null; } if ( @@ -89,13 +89,14 @@ public function get($i) * * @param integer $i Fragment index * - * @return integer + * @return integer|null */ public function getHalved($i) { if (!isset($this->fragments[$i])) { - return; + return null; } + if (!isset($this->sizes[$i])) { return $this->fragments[$i]; } @@ -115,7 +116,7 @@ public function canBeHalved($i) !isset($this->fragments[$i]) || !isset($this->sizes[$i]) ) { - return; + return null; } $half = (int)floor($this->sizes[$i] / 2); @@ -198,7 +199,7 @@ public function withHalveds(array $halved) { $res = clone $this; - $this->halved = $this->fillArrayInput($halved); + $this->halved = $this->fillArrayInput($halved, false); return $res; }