source/Stream/GeneratorBasedReader.php
Function __destruct
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
Open
public function __destruct() {
foreach ($this->filters as $class => $instance) {
try {
if (is_resource($instance)) {
stream_filter_remove($instance);
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Missing class import via use statement (line '81', column '14'). Open
Open
throw new \InvalidArgumentException($error);
- Read upRead up
- Exclude checks
MissingImport
Since: 2.7.0
Importing all external classes in a file through use statements makes them clearly visible.
Example
function make() {
return new \stdClass();
}
Source http://phpmd.org/rules/cleancode.html#MissingImport
Opening brace of a class must be on the line after the definition Open
Open
class GeneratorBasedReader implements ReaderInterface {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
use LogsIfAvailableTrait;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
*/
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
* @var string[]
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
SkipEndOfCommentedLineFilter::class => false,
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
$this->filters = [
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
/**
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
if (!is_resource($stream)) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
$this->stream,
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
public function __destruct() {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
*/
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
protected function __construct() {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
/**
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
stream_filter_remove($instance);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
TrimSpacesLeftFilter::class => false,
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
SkipUnsupportedDirectivesFilter::class => false,
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
$this->log('Failed to remove filter "{class}": {message}', [
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
private array $filters;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
/** @note order matters */
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
SkipEmptyLinesFilter::class => false,
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
*/
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
private $stream;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
* @param string $input
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
* @return static
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
SkipCommentedLinesFilter::class => false,
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
SkipDirectivesWithInvalidValuesFilter::class => false,
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
'message' => $throwable->getMessage(),
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
* @link https://www.php.net/manual/en/function.stream-filter-append.php#84637
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
try {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
/**
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
$stream = tmpfile();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
$error = sprintf('Argument must be a valid resource type. %s given.', gettype($stream));
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
protected function setStream($stream): GeneratorBasedReader {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
stream_filter_register($filterClass::NAME, $filterClass);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
STREAM_FILTER_READ,
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
* @param string $encoding
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
public function setEncoding(string $encoding) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
while (!feof($this->stream)) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
if (is_resource($instance)) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
$reader = new GeneratorBasedReader();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
public function getContentRaw(): string {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
public function getContentIterated(): \Generator {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
rewind($stream);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
public static function fromStream($stream): self {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
/**
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
*
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
foreach ($this->filters as $filterClass => & $filter) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
*/
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
if (strtoupper($encoding) === RobotsTxtParser::DEFAULT_ENCODING) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
return;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
$filter = stream_filter_append(
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
$line = fgets($this->stream);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
$filterClass::NAME,
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
return $this;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
rewind($this->stream);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
rewind($this->stream);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
'class' => $class,
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
$this->log(WarmingMessages::ENCODING_NOT_UTF8, [], LogLevel::WARNING);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
} catch (\Throwable $throwable) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
fclose($this->stream);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
$reader = new GeneratorBasedReader();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
fwrite($stream, $input);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
fseek($stream, 0);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
$reader->log(WarmingMessages::STRING_INIT_DEPRECATE);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
$this->stream = $stream;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
$this->filters['iconv'] = stream_filter_prepend($this->stream, $filterName, STREAM_FILTER_READ);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
* @TODO check on composer install if we have filters available
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
$filterName = 'convert.iconv.' . $encoding . '/utf-8';
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
return stream_get_contents($this->stream);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
return $reader->setStream($stream);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
// convert encoding
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
yield $line;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
]);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
if (is_resource($this->stream)) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
public static function fromString(string $input = ''): self {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
throw new \InvalidArgumentException($error);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
['logger' => $this->logger] // pass logger to filters
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
$this->log('Adding encoding filter ' . $filterName);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
if (false !== $line) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
foreach ($this->filters as $class => $instance) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
*
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
return $reader->setStream($stream);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Open
}
- Exclude checks
Opening brace should be on a new line Open
Open
protected function __construct() {
- Exclude checks
Opening brace should be on a new line Open
Open
public function __destruct() {
- Exclude checks
Opening brace should be on a new line Open
Open
public function getContentIterated(): \Generator {
- Exclude checks
Opening brace should be on a new line Open
Open
public static function fromStream($stream): self {
- Exclude checks
Opening brace should be on a new line Open
Open
protected function setStream($stream): GeneratorBasedReader {
- Exclude checks
Opening brace should be on a new line Open
Open
public static function fromString(string $input = ''): self {
- Exclude checks
Opening brace should be on a new line Open
Open
public function getContentRaw(): string {
- Exclude checks
Opening brace should be on a new line Open
Open
public function setEncoding(string $encoding) {
- Exclude checks