Skip to content

Commit

Permalink
Slightly reduce comparing and calling methods for trivial optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
zonuexe authored Apr 17, 2024
1 parent d9bdbc2 commit c792050
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ private function processStmtNode(
}

$isFromTrait = $stmt->getAttribute('originalTraitMethodName') === '__construct';
if ($stmt->name->toLowerString() === '__construct' || $isFromTrait) {
if ($isFromTrait || $stmt->name->toLowerString() === '__construct') {
foreach ($stmt->params as $param) {
if ($param->flags === 0) {
continue;
Expand Down
14 changes: 6 additions & 8 deletions src/File/FileHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use function ltrim;
use function preg_match;
use function rtrim;
use function str_ends_with;
use function str_replace;
use function str_starts_with;
use function strlen;
Expand Down Expand Up @@ -36,14 +37,13 @@ public function getWorkingDirectory(): string
public function absolutizePath(string $path): string
{
if (DIRECTORY_SEPARATOR === '/') {
if (substr($path, 0, 1) === '/') {
return $path;
}
} else {
if (substr($path, 1, 1) === ':') {
if (str_starts_with($path, '/')) {
return $path;
}
} elseif (substr($path, 1, 1) === ':') {
return $path;
}

if (preg_match('~^[a-z0-9+\-.]+://~i', $path) === 1) {
return $path;
}
Expand Down Expand Up @@ -87,12 +87,10 @@ public function normalizePath(string $originalPath, string $directorySeparator =
continue;
}
if ($pathPart === '..') {
/** @var string $removedPart */
$removedPart = array_pop($normalizedPathParts);
if ($scheme === 'phar' && substr($removedPart, -5) === '.phar') {
if ($scheme === 'phar' && $removedPart !== null && str_ends_with($removedPart, '.phar')) {
$scheme = null;
}

} else {
$normalizedPathParts[] = $pathPart;
}
Expand Down
2 changes: 1 addition & 1 deletion src/File/FuzzyRelativePathHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct(
$pathBeginning = null;
$pathToTrimArray = null;
$trimBeginning = static function (string $path): array {
if (substr($path, 0, 1) === '/') {
if (str_starts_with($path, '/')) {
return [
'/',
substr($path, 1),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ private function skipComment(): void
private function skipToNewline(): void
{
while ($this->index < $this->len) {
if ($this->contents[$this->index] === "\r" || $this->contents[$this->index] === "\n") {
if (in_array($this->contents[$this->index], ["\r", "\n"], true)) {
return;
}
$this->index += 1;
Expand Down
2 changes: 1 addition & 1 deletion src/Reflection/InitializerExprTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ public function getDivType(Expr $left, Expr $right, callable $getTypeCallback):
throw new ShouldNotHappenException();
}

if ($rightNumberType->getValue() === 0 || $rightNumberType->getValue() === 0.0) {
if (in_array($rightNumberType->getValue(), [0, 0.0], true)) {
return new ErrorType();
}

Expand Down
3 changes: 2 additions & 1 deletion src/Type/IntersectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use function ksort;
use function md5;
use function sprintf;
use function str_starts_with;
use function strlen;
use function substr;

Expand Down Expand Up @@ -388,7 +389,7 @@ private function describeItself(VerbosityLevel $level, bool $skipAccessoryTypes)
}

if (
substr($typeDescription, 0, strlen('array<')) === 'array<'
str_starts_with($typeDescription, 'array<')
&& in_array('array', $skipTypeNames, true)
) {
$nonEmpty = false;
Expand Down
4 changes: 2 additions & 2 deletions src/Type/ObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,12 +375,12 @@ public function isSuperTypeOf(Type $type): TrinaryLogic
}

$reflectionProvider = ReflectionProviderStaticAccessor::getInstance();
$thisClassReflection = $this->getClassReflection();

if ($this->getClassReflection() === null || !$reflectionProvider->hasClass($thatClassNames[0])) {
if ($thisClassReflection === null || !$reflectionProvider->hasClass($thatClassNames[0])) {
return self::$superTypes[$thisDescription][$description] = TrinaryLogic::createMaybe();
}

$thisClassReflection = $this->getClassReflection();
$thatClassReflection = $reflectionProvider->getClass($thatClassNames[0]);

if ($thisClassReflection->isTrait() || $thatClassReflection->isTrait()) {
Expand Down

0 comments on commit c792050

Please sign in to comment.