Skip to content
Snippets Groups Projects

[RTKO] default volatile on setupOpac

Merged Sebastien ANDRE requested to merge fix_default_volatile_on_setup into master
Compare and Show latest version
12 files
+ 0
1027
Preferences
Compare changes
Files
12
<?php
/**
* Copyright (c) 2012-2024, Agence Française Informatique (AFI). All rights reserved.
*
* BOKEH is free software; you can redistribute it and/or modify
* it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
* the Free Software Foundation.
*
* There are special exceptions to the terms and conditions of the AGPL as it
* is applied to this software (see README file).
*
* BOKEH is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* along with BOKEH; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class Class_Migration_SplitTestsFiles_TestClass
{
const OFFICIAL = "<?php
/**
* Copyright (c) 2012-2024, Agence Française Informatique (AFI). All rights reserved.
*
* BOKEH is free software; you can redistribute it and/or modify
* it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
* the Free Software Foundation.
*
* There are special exceptions to the terms and conditions of the AGPL as it
* is applied to this software (see README file).
*
* BOKEH is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* along with BOKEH; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
\n";
protected string $_name = '';
protected string $_extend = '';
protected bool $_is_abstract = false;
protected bool $_is_opened = false;
protected bool $_is_closed = false;
protected array $_functions = [];
protected ?Class_Migration_SplitTestsFiles_TestFunction $_current_function = null;
protected ?Class_Migration_SplitTestsFiles_TestFunction $_setup = null;
protected ?Class_Migration_SplitTestsFiles_TestFunction $_teardown = null;
protected ?Class_Migration_SplitTestsFiles_TestComment $_comment = null;
public function addContent(string $content): self
{
if ($this->isClosed())
return $this;
return ('' !== $this->_name && '' !== $this->_extend)
? $this->_addFunction($content)
: $this->_addClass($content);
}
public function isClosed(): bool
{
return $this->_is_opened && $this->_is_closed;
}
public function contents(): string
{
if ($this->_teardown)
$this->_functions = [$this->_teardown, ...$this->_functions];
if ($this->_setup)
$this->_functions = [$this->_setup, ...$this->_functions];
$this->_functions = array_filter($this->_functions,
fn($func) => ! $func->isEmpty());
return $this->isClosed()
? $this->_contentComments() . $this->_contentClass() . $this->_contentFunctions()
: '';
}
protected function _contentComments(): string
{
return static::OFFICIAL . $this->_comment()->contents();
}
protected function _contentClass(): string
{
return Class_Migration_SplitTestsFiles::indent(0)
. ($this->_is_abstract ? 'abstract ' : '')
. 'class ' . $this->_name
. ' extends ' . $this->_extend;
}
protected function _contentFunctions(): string
{
return Class_Migration_SplitTestsFiles::indent(0) . "{\n\n"
. implode("\n\n", array_map(fn($func) => $func->contents(), $this->_functions))
. Class_Migration_SplitTestsFiles::indent(0, false) . "}\n";
}
protected function _addClass(string $content): self
{
if ( ! preg_match('/\s*(abstract)?\s*class\s+(\w+)\s+extends\s+(\w+)\s*(\{)?\s*/i',
$content, $matches))
return $this->_addComment($content);
$this->_is_abstract = ('' !== ($matches[1] ?? ''));
$this->_name = ($matches[2] ?? '');
$this->_extend = ($matches[3] ?? '');
$this->_is_opened = ('' !== ($matches[4] ?? ''));
return $this;
}
protected function _addFunction(string $content): self
{
if ( ! $this->_is_opened)
{
$this->_is_opened = Class_Migration_SplitTestsFiles::isOpenedContent($content);
return $this;
}
if ( ! $this->_current_function)
$this->_current_function = new Class_Migration_SplitTestsFiles_TestFunction;
if ($this->_doClosed($content))
return $this->_reset();
return ($this->_current_function
->addContent($content)
->isClosed())
? $this->_reset()
: $this;
}
protected function _doClosed(string $content): bool
{
if ($this->_currFunctionIsClosed()
&& Class_Migration_SplitTestsFiles::isClosedContent($content))
$this->_is_closed = true;
return $this->_is_closed;
}
protected function _currFunctionIsClosed(): bool
{
return ! $this->_current_function
|| $this->_current_function->isEmpty()
|| $this->_current_function->isClosed();
}
protected function _reset(): self
{
$function = $this->_current_function;
$this->_current_function = null;
if ($function->isSetup())
{
$this->_setup = $function;
return $this;
}
if ($function->isTeardown())
{
$this->_teardown = $function;
return $this;
}
$this->_functions [] = $function;
return $this;
}
protected function _addComment(string $content): self
{
$this->_comment()->addContent($content);
return $this;
}
protected function _comment(): Class_Migration_SplitTestsFiles_TestComment
{
return $this->_comment ??= new Class_Migration_SplitTestsFiles_TestComment;
}
}