Skip to content

Commit

Permalink
Perform (mostly) safe rector code migrations
Browse files Browse the repository at this point in the history
Importing names, replaces substr/strpos, stripping unnecessary comments
and surc
  • Loading branch information
usox committed Nov 2, 2023
1 parent aefa71b commit 20619b3
Show file tree
Hide file tree
Showing 75 changed files with 230 additions and 1,359 deletions.
10 changes: 5 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ parameters:
count: 1
path: src/Dom/SaxXmlParser.php

-
message: "#^Parameter \\#1 \\$value of function count expects array\\|Countable, array\\<int, string\\>\\|false given\\.$#"
count: 1
path: src/Dom/SaxXmlParser.php

-
message: "#^Parameter \\#2 \\$array of function implode expects array\\|null, array\\<int\\<0, max\\>, string\\>\\|false given\\.$#"
count: 1
Expand Down Expand Up @@ -70,6 +65,11 @@ parameters:
count: 1
path: src/Php/Attribute/TAL/Repeat.php

-
message: "#^Parameter \\#2 \\$callback of function preg_replace_callback expects callable\\(array\\<int\\|string, string\\>\\)\\: string, Closure\\(array\\<int, string\\>\\)\\: string given\\.$#"
count: 2
path: src/Php/State.php

-
message: "#^Instanceof between mixed and DOMNodeList will always evaluate to false\\.$#"
count: 1
Expand Down
23 changes: 13 additions & 10 deletions src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
use ArrayAccess;
use BadMethodCallException;
use Countable;
use PhpTal\Exception\ConfigurationException;
use PhpTal\Exception\InvalidVariableNameException;
use PhpTal\Exception\VariableNotFoundException;
use stdClass;

/**
Expand Down Expand Up @@ -142,7 +145,7 @@ public function setDocType(string $doctype, bool $called_from_macro): void
$this->_parentContext->setDocType($doctype, $called_from_macro);
} elseif ($this->_echoDeclarations) {
if ($called_from_macro) {
throw new Exception\ConfigurationException(
throw new ConfigurationException(
'Executed macro in file with DOCTYPE when using echoExecute(). This is not supported yet. ' .
'Remove DOCTYPE or use PHPTAL->execute().'
);
Expand Down Expand Up @@ -175,7 +178,7 @@ public function setXmlDeclaration(string $xmldec, bool $called_from_macro): void
$this->_parentContext->setXmlDeclaration($xmldec, $called_from_macro);
} elseif ($this->_echoDeclarations) {
if ($called_from_macro) {
throw new Exception\ConfigurationException(
throw new ConfigurationException(
'Executed macro in file with XML declaration when using echoExecute(). This is not supported yet.' .
' Remove XML declaration or use PHPTAL->execute().'
);
Expand Down Expand Up @@ -309,7 +312,7 @@ public function set(string $varname, mixed $value): void
public function __set(string $varname, mixed $value): void
{
if (preg_match('/^_|\s/', $varname)) {
throw new Exception\InvalidVariableNameException(
throw new InvalidVariableNameException(
'Template variable error \'' . $varname . '\' must not begin with underscore or contain spaces'
);
}
Expand Down Expand Up @@ -345,7 +348,7 @@ public function __get(string $varname): mixed
return null;
}

throw new Exception\VariableNotFoundException("Unable to find variable '$varname' in current scope");
throw new VariableNotFoundException("Unable to find variable '$varname' in current scope");
}

/**
Expand All @@ -366,17 +369,17 @@ private static function pathError(mixed $base, string $path, string $current, ?s
}

if (is_array($base)) {
throw new Exception\VariableNotFoundException(
throw new VariableNotFoundException(
"Array {$basename} doesn't have key named '$current' $pathinfo"
);
}
if (is_object($base)) {
throw new Exception\VariableNotFoundException(
ucfirst(get_class($base)) .
throw new VariableNotFoundException(
ucfirst($base::class) .
" object {$basename} doesn't have method/property named '$current' $pathinfo"
);
}
throw new Exception\VariableNotFoundException(
throw new VariableNotFoundException(
trim("Attempt to read property '$current' $pathinfo from " . gettype($base) . " value {$basename}")
);
}
Expand Down Expand Up @@ -461,7 +464,7 @@ public static function path($base, string $path, bool $nothrow = null): mixed
try {
$base = $base->__call($current, []);
continue;
} catch (BadMethodCallException $e) {
} catch (BadMethodCallException) {
// noop
}
}
Expand All @@ -482,7 +485,7 @@ public static function path($base, string $path, bool $nothrow = null): mixed
// array handling
if (is_array($base)) {
// key or index
if (array_key_exists((string)$current, $base)) {
if (array_key_exists($current, $base)) {
$base = $base[$current];
continue;
}
Expand Down
14 changes: 2 additions & 12 deletions src/DefaultKeyword.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,23 @@

use Countable;
use stdClass;
use Stringable;

/**
* Representation of the template 'default' keyword
*
* @package PHPTAL
*/
class DefaultKeyword implements Countable
class DefaultKeyword implements Countable, Stringable
{
/**
* @return string
*/
public function __toString(): string
{
return "''";
}

/**
* @return int
*/
public function count(): int
{
return 1;
}

/**
* @return stdClass
*/
public function jsonSerialize(): stdClass
{
return new stdClass();
Expand Down
47 changes: 5 additions & 42 deletions src/Dom/Attr.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,10 @@
*/
class Attr
{
public const HIDDEN = -1;
public const NOT_REPLACED = 0;
public const VALUE_REPLACED = 1;
public const FULLY_REPLACED = 2;

/**
* @var string
*/
private $value_escaped;

/**
* @var string
*/
private $qualified_name;

/**
* @var string
*/
private $namespace_uri;

/**
* @var string
*/
private $encoding;
final public const HIDDEN = -1;
final public const NOT_REPLACED = 0;
final public const VALUE_REPLACED = 1;
final public const FULLY_REPLACED = 2;

/**
* attribute's value can be overriden with a variable
Expand All @@ -63,12 +43,8 @@ class Attr
* @param string $value_escaped value with HTML-escaping
* @param string $encoding character encoding used by the value
*/
public function __construct(string $qualified_name, string $namespace_uri, ?string $value_escaped, string $encoding)
public function __construct(private string $qualified_name, private string $namespace_uri, private ?string $value_escaped, private string $encoding)
{
$this->value_escaped = $value_escaped;
$this->qualified_name = $qualified_name;
$this->namespace_uri = $namespace_uri;
$this->encoding = $encoding;
}

/**
Expand Down Expand Up @@ -106,8 +82,6 @@ public function getLocalName(): string

/**
* Returns true if this attribute is ns declaration (xmlns="...")
*
* @return bool
*/
public function isNamespaceDeclaration(): bool
{
Expand All @@ -117,8 +91,6 @@ public function isNamespaceDeclaration(): bool

/**
* get value as plain text
*
* @return string
*/
public function getValue(): string
{
Expand All @@ -127,7 +99,6 @@ public function getValue(): string

/**
* set plain text as value
* @param string $val
*/
public function setValue(string $val): void
{
Expand Down Expand Up @@ -160,8 +131,6 @@ public function setValueEscaped($value_escaped): void

/**
* set PHP code as value of this attribute. Code is expected to echo the value.
*
* @param string $code
*/
private function setPHPCode(string $code): void
{
Expand All @@ -178,8 +147,6 @@ public function hide(): void

/**
* generate value of this attribute from variable
*
* @param string $phpVariable
*/
public function overwriteValueWithVariable(string $phpVariable): void
{
Expand All @@ -190,8 +157,6 @@ public function overwriteValueWithVariable(string $phpVariable): void

/**
* generate complete syntax of this attribute using variable
*
* @param string $phpVariable
*/
public function overwriteFullWithVariable(string $phpVariable): void
{
Expand All @@ -202,8 +167,6 @@ public function overwriteFullWithVariable(string $phpVariable): void

/**
* use any PHP code to generate this attribute's value
*
* @param string $code
*/
public function overwriteValueWithCode(string $code): void
{
Expand Down
2 changes: 0 additions & 2 deletions src/Dom/CDATASection.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ class CDATASection extends Node
{
/**
* use CodeWriter to compile this element to PHP code
*
* @param CodeWriter $codewriter
*/
public function generateCode(CodeWriter $codewriter): void
{
Expand Down
2 changes: 0 additions & 2 deletions src/Dom/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ class Comment extends Node
{
/**
* use CodeWriter to compile this element to PHP code
*
* @param CodeWriter $codewriter
*/
public function generateCode(CodeWriter $codewriter): void
{
Expand Down
20 changes: 0 additions & 20 deletions src/Dom/Defs.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,8 @@ protected function __construct()
* true if it's empty in XHTML (e.g. <img/>)
* it will assume elements with no namespace may be XHTML too.
*
* @param string $namespace_uri
* @param string $local_name local name of the tag
*
* @return bool
*/
public function isEmptyTagNS(string $namespace_uri, string $local_name): bool
{
Expand All @@ -174,8 +172,6 @@ public function prefixToNamespaceURI($prefix): ?string
* gives typical prefix for given (built-in) namespace
*
* @param string $uri
*
* @return bool
*/
public function namespaceURIToPrefix($uri): bool
{
Expand All @@ -196,8 +192,6 @@ public function getPredefinedPrefixes(): array
* Returns true if the attribute is an xhtml boolean attribute.
*
* @param string $att local name
*
* @return bool
*/
public function isBooleanAttribute(string $att): bool
{
Expand All @@ -208,10 +202,7 @@ public function isBooleanAttribute(string $att): bool
* true if elements content is parsed as CDATA in text/html
* and also accepts /* * / as comments.
*
* @param string $namespace_uri
* @param string $local_name
*
* @return bool
*/
public function isCDATAElementInHTML(string $namespace_uri, string $local_name): bool
{
Expand All @@ -227,8 +218,6 @@ public function isCDATAElementInHTML(string $namespace_uri, string $local_name):
*
* @param string $namespace_uri
* @param string $local_name
*
* @return bool
*/
public function isValidAttributeNS(string $namespace_uri, string $local_name): bool
{
Expand All @@ -243,9 +232,7 @@ public function isValidAttributeNS(string $namespace_uri, string $local_name): b
/**
* is URI registered (built-in) namespace
*
* @param string $namespace_uri
*
* @return bool
*/
public function isHandledNamespace(string $namespace_uri): bool
{
Expand All @@ -260,8 +247,6 @@ public function isHandledNamespace(string $namespace_uri): bool
*
* @param string $qname
* @param string $value
*
* @return bool
*/
public function isHandledXmlNs(string $qname, string $value): bool
{
Expand All @@ -271,10 +256,7 @@ public function isHandledXmlNs(string $qname, string $value): bool
/**
* return objects that holds information about given TAL attribute
*
* @param string $namespace_uri
* @param string $local_name
*
* @return TalNamespaceAttribute
*/
public function getNamespaceAttribute(string $namespace_uri, string $local_name): TalNamespaceAttribute
{
Expand All @@ -284,8 +266,6 @@ public function getNamespaceAttribute(string $namespace_uri, string $local_name)

/**
* Register a \PhpTal\TalNamespace and its attribute into PHPTAL.
*
* @param TalNamespace $ns
*/
public function registerNamespace(TalNamespace $ns): void
{
Expand Down
Loading

0 comments on commit 20619b3

Please sign in to comment.