Skip to content

Commit

Permalink
Convert immutable array properties to constants, can drop @vars
Browse files Browse the repository at this point in the history
  • Loading branch information
spaze committed Dec 31, 2023
1 parent 417d66b commit b816100
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 56 deletions.
18 changes: 8 additions & 10 deletions site/app/DateTime/DateTimeFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ class DateTimeFormatter
private const INTERVAL_FORMAT_SEPARATOR = 2;
private const INTERVAL_FORMAT_END = 3;

/** @var array<string, array<string, array{1:string, 2:array<int,string>, 3:array<int, string>, 4?:array<int, string>}>> */
private array $localDateFormat = [
private const LOCAL_DATE_FORMAT = [
'en_US' => [
self::DATE_DAY => [
self::NO_INTERVAL => 'MMMM d, y',
Expand Down Expand Up @@ -93,8 +92,7 @@ class DateTimeFormatter
],
];

/** @var array<string, array<int, ?string>> */
private array $comparisonFormat = [
private const COMPARISON_FORMAT = [
self::DATE_DAY => [
self::NO_INTERVAL => 'Ymd',
self::INTERVAL => 'Ym',
Expand Down Expand Up @@ -122,18 +120,18 @@ private function localeDate(DateTimeInterface $start, ?DateTimeInterface $end, s

$formatter = new IntlDateFormatter($locale, IntlDateFormatter::NONE, IntlDateFormatter::NONE);
if ($end === null || $this->sameDates($start, $end, $type, self::NO_INTERVAL)) {
$formatter->setPattern($this->localDateFormat[$locale][$type][self::NO_INTERVAL]);
$formatter->setPattern(self::LOCAL_DATE_FORMAT[$locale][$type][self::NO_INTERVAL]);
$result = $formatter->format($start);
} else {
if ($this->sameDates($start, $end, $type, self::INTERVAL)) {
$format = $this->localDateFormat[$locale][$type][self::INTERVAL];
$format = self::LOCAL_DATE_FORMAT[$locale][$type][self::INTERVAL];
} elseif (
isset($this->localDateFormat[$locale][$type][self::INTERVAL_BOUNDARIES])
isset(self::LOCAL_DATE_FORMAT[$locale][$type][self::INTERVAL_BOUNDARIES])
&& !$this->sameDates($start, $end, $type, self::INTERVAL_BOUNDARY)
) {
$format = $this->localDateFormat[$locale][$type][self::INTERVAL_BOUNDARIES];
$format = self::LOCAL_DATE_FORMAT[$locale][$type][self::INTERVAL_BOUNDARIES];
} else {
$format = $this->localDateFormat[$locale][$type][self::INTERVAL_BOUNDARY];
$format = self::LOCAL_DATE_FORMAT[$locale][$type][self::INTERVAL_BOUNDARY];
}

$formatter->setPattern($format[self::INTERVAL_FORMAT_START]);
Expand Down Expand Up @@ -177,7 +175,7 @@ public function localeIntervalMonth(DateTimeInterface $start, DateTimeInterface

private function sameDates(DateTimeInterface $start, DateTimeInterface $end, string $type, int $level): bool
{
return isset($this->comparisonFormat[$type][$level]) && ($start->format($this->comparisonFormat[$type][$level]) === $end->format($this->comparisonFormat[$type][$level]));
return isset(self::COMPARISON_FORMAT[$type][$level]) && ($start->format(self::COMPARISON_FORMAT[$type][$level]) === $end->format(self::COMPARISON_FORMAT[$type][$level]));
}

}
15 changes: 6 additions & 9 deletions site/app/EasterEgg/WinterIsComing.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,21 @@
class WinterIsComing
{

/** @var array<int, string> */
private array $emails = [
private const EMAILS = [
'[email protected]',
'[email protected]',
'[email protected]',
];

/** @var array<int, string> */
private array $hosts = [
private const HOSTS = [
'burpcollaborator.net',
'mailrez.com',
'mailto.plus',
'ourtimesupport.com',
'ssemarketing.net',
];

/** @var array<int, string> */
private array $streets = [
private const STREETS = [
'34 Watts road',
];

Expand All @@ -44,8 +41,8 @@ public function ruleEmail(): callable
if (
is_string($input->getValue())
&& (
Arrays::contains($this->emails, $input->getValue())
|| Strings::match($input->getValue(), '/@(' . implode('|', array_map('preg_quote', $this->hosts)) . ')$/')
Arrays::contains(self::EMAILS, $input->getValue())
|| Strings::match($input->getValue(), '/@(' . implode('|', array_map('preg_quote', self::HOSTS)) . ')$/')
)
) {
$this->sendSyntaxError($input);
Expand All @@ -61,7 +58,7 @@ public function ruleEmail(): callable
public function ruleStreet(): callable
{
return function (TextInput $input) {
if (Arrays::contains($this->streets, $input->getValue())) {
if (Arrays::contains(self::STREETS, $input->getValue())) {
$this->sendSyntaxError($input);
}
return true;
Expand Down
30 changes: 14 additions & 16 deletions site/app/Media/SupportedImageFileFormats.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@
class SupportedImageFileFormats
{

/** @var non-empty-array<string, string> */
private array $supportedMainImages = [
private const SUPPORTED_MAIN_IMAGES = [
'image/gif' => 'gif',
'image/png' => 'png',
'image/jpeg' => 'jpg',
];

/** @var non-empty-array<string, string> */
private array $supportedAlternativeImages = [
private const SUPPORTED_ALTERNATIVE_IMAGES = [
'image/webp' => 'webp',
];

Expand All @@ -28,10 +26,10 @@ class SupportedImageFileFormats
*/
public function getMainExtensionByContentType(string $contentType): string
{
if (!isset($this->supportedMainImages[$contentType])) {
throw new UnsupportedContentTypeException($contentType, $this->supportedMainImages);
if (!isset(self::SUPPORTED_MAIN_IMAGES[$contentType])) {
throw new UnsupportedContentTypeException($contentType, self::SUPPORTED_MAIN_IMAGES);
}
return $this->supportedMainImages[$contentType];
return self::SUPPORTED_MAIN_IMAGES[$contentType];
}


Expand All @@ -40,10 +38,10 @@ public function getMainExtensionByContentType(string $contentType): string
*/
public function getAlternativeExtensionByContentType(string $contentType): string
{
if (!isset($this->supportedAlternativeImages[$contentType])) {
throw new UnsupportedContentTypeException($contentType, $this->supportedAlternativeImages);
if (!isset(self::SUPPORTED_ALTERNATIVE_IMAGES[$contentType])) {
throw new UnsupportedContentTypeException($contentType, self::SUPPORTED_ALTERNATIVE_IMAGES);
}
return $this->supportedAlternativeImages[$contentType];
return self::SUPPORTED_ALTERNATIVE_IMAGES[$contentType];
}


Expand All @@ -52,9 +50,9 @@ public function getAlternativeExtensionByContentType(string $contentType): strin
*/
public function getAlternativeContentTypeByExtension(string $extension): string
{
$types = array_flip($this->supportedAlternativeImages);
$types = array_flip(self::SUPPORTED_ALTERNATIVE_IMAGES);
if (!isset($types[$extension])) {
throw new ExtensionWithNoContentTypeException($extension, $this->supportedAlternativeImages);
throw new ExtensionWithNoContentTypeException($extension, self::SUPPORTED_ALTERNATIVE_IMAGES);
}
return $types[$extension];
}
Expand All @@ -65,7 +63,7 @@ public function getAlternativeContentTypeByExtension(string $extension): string
*/
public function getMainContentTypes(): array
{
return array_keys($this->supportedMainImages);
return array_keys(self::SUPPORTED_MAIN_IMAGES);
}


Expand All @@ -74,7 +72,7 @@ public function getMainContentTypes(): array
*/
public function getAlternativeContentTypes(): array
{
return array_keys($this->supportedAlternativeImages);
return array_keys(self::SUPPORTED_ALTERNATIVE_IMAGES);
}


Expand All @@ -83,7 +81,7 @@ public function getAlternativeContentTypes(): array
*/
public function getMainExtensions(): array
{
return array_values($this->supportedMainImages);
return array_values(self::SUPPORTED_MAIN_IMAGES);
}


Expand All @@ -92,7 +90,7 @@ public function getMainExtensions(): array
*/
public function getAlternativeExtensions(): array
{
return array_values($this->supportedAlternativeImages);
return array_values(self::SUPPORTED_ALTERNATIVE_IMAGES);
}

}
36 changes: 15 additions & 21 deletions site/app/Pulse/Passwords/Rating.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,25 @@
class Rating
{

/** @var string[] */
private array $slowHashes = [
private const SLOW_HASHES = [
'argon2',
'bcrypt',
'pbkdf2',
'scrypt',
];

/** @var string[] */
private array $insecure = [
private const INSECURE = [
'plaintext',
'encrypted',
];

/** @var string[] */
private array $visibleDisclosures = [
private const VISIBLE_DISCLOSURES = [
'docs',
'faq',
'signup-page',
];

/** @var string[] */
private array $invisibleDisclosures = [
private const INVISIBLE_DISCLOSURES = [
'blog',
'site-independent',
'facebook-independent',
Expand All @@ -45,8 +41,7 @@ class Rating
'comment',
];

/** @var array<string, string> */
private array $rating = [
private const RATING = [
RatingGrade::A->name => 'Site uses a slow hashing function, this is disclosed "on-site", in the docs, FAQ, etc.',
RatingGrade::B->name => 'A slow hashing function is used but such info is "invisible", hidden in a blog post or a talk, or on social media.',
RatingGrade::C->name => 'Passwords hashed with an unsuitable function but at least they are salted and stretched with multiple iterations.',
Expand All @@ -55,8 +50,7 @@ class Rating
RatingGrade::F->name => 'Passwords stored in plaintext, in their original, readable form, or passwords encrypted instead of hashed.',
];

/** @var array<string, string|null> */
private array $recommendations = [
private const RECOMMENDATIONS = [
RatingGrade::A->name => null,
RatingGrade::B->name => 'Publish storage and hashing info details "visibly":[link:Pulse:PasswordsStorages:Rating#on-site] (e.g. in the docs or FAQ), then let me know.',
RatingGrade::C->name => 'Start using "&quot;slow&quot; hashes":[link:Pulse:PasswordsStorages:Rating#slow-hashes], don\'t forget to "re-hash existing passwords":[blog:upgrading-existing-password-hashes], publish hashing info "visibly":[link:Pulse:PasswordsStorages:Rating#on-site], then let me know.',
Expand All @@ -78,13 +72,13 @@ class Rating
*/
public function get(Algorithm $algo): RatingGrade
{
if (in_array($algo->getAlias(), $this->slowHashes, true)) {
foreach ($this->visibleDisclosures as $disclosure) {
if (in_array($algo->getAlias(), self::SLOW_HASHES, true)) {
foreach (self::VISIBLE_DISCLOSURES as $disclosure) {
if ($algo->hasDisclosureType($disclosure)) {
return RatingGrade::A;
}
}
foreach ($this->invisibleDisclosures as $disclosure) {
foreach (self::INVISIBLE_DISCLOSURES as $disclosure) {
if ($algo->hasDisclosureType($disclosure)) {
return RatingGrade::B;
}
Expand All @@ -94,7 +88,7 @@ public function get(Algorithm $algo): RatingGrade
return RatingGrade::C;
} elseif ($algo->isSalted()) {
return RatingGrade::D;
} elseif (!in_array($algo->getAlias(), $this->insecure, true)) {
} elseif (!in_array($algo->getAlias(), self::INSECURE, true)) {
return RatingGrade::E;
} else {
return RatingGrade::F;
Expand All @@ -107,7 +101,7 @@ public function get(Algorithm $algo): RatingGrade
*/
public function getRecommendation(RatingGrade $rating): ?string
{
return $this->recommendations[$rating->name];
return self::RECOMMENDATIONS[$rating->name];
}


Expand Down Expand Up @@ -142,7 +136,7 @@ public function getRatings(): array
*/
public function getRatingGuide(): array
{
return $this->rating;
return self::RATING;
}


Expand All @@ -153,7 +147,7 @@ public function getRatingGuide(): array
*/
public function getSlowHashes(): array
{
return $this->slowHashes;
return self::SLOW_HASHES;
}


Expand All @@ -164,7 +158,7 @@ public function getSlowHashes(): array
*/
public function getInvisibleDisclosures(): array
{
return $this->invisibleDisclosures;
return self::INVISIBLE_DISCLOSURES;
}


Expand All @@ -175,7 +169,7 @@ public function getInvisibleDisclosures(): array
*/
public function getVisibleDisclosures(): array
{
return $this->visibleDisclosures;
return self::VISIBLE_DISCLOSURES;
}

}

0 comments on commit b816100

Please sign in to comment.