php-cs run: fixed code style for new/changed files

This commit is contained in:
Paolo Agostinetto 2017-02-20 21:05:25 +01:00
parent a0321fd6fd
commit 9785f926c1
2 changed files with 121 additions and 134 deletions

View File

@ -107,6 +107,7 @@ class Ods extends BaseReader implements IReader
* @param string $pFilename
*
* @throws Exception
*
* @return string[]
*/
public function listWorksheetNames($pFilename)
@ -163,6 +164,7 @@ class Ods extends BaseReader implements IReader
* @param string $pFilename
*
* @throws Exception
*
* @return array
*/
public function listWorksheetInfo($pFilename)
@ -173,7 +175,7 @@ class Ods extends BaseReader implements IReader
$zipClass = \PhpOffice\PhpSpreadsheet\Settings::getZipClass();
/** @var \ZipArchive $zip */
/** @var \ZipArchive $zip */
$zip = new $zipClass();
if (!$zip->open($pFilename)) {
throw new Exception('Could not open ' . $pFilename . ' for reading! Error opening file.');
@ -407,25 +409,23 @@ class Ods extends BaseReader implements IReader
\PhpOffice\PhpSpreadsheet\Settings::getLibXmlLoaderOptions()
);
$officeNs = $dom->lookupNamespaceUri("office");
$tableNs = $dom->lookupNamespaceUri("table");
$textNs = $dom->lookupNamespaceUri("text");
$xlinkNs = $dom->lookupNamespaceUri("xlink");
$officeNs = $dom->lookupNamespaceUri('office');
$tableNs = $dom->lookupNamespaceUri('table');
$textNs = $dom->lookupNamespaceUri('text');
$xlinkNs = $dom->lookupNamespaceUri('xlink');
$spreadsheets = $dom->getElementsByTagNameNS($officeNs, "body")
$spreadsheets = $dom->getElementsByTagNameNS($officeNs, 'body')
->item(0)
->getElementsByTagNameNS($officeNs, "spreadsheet");
->getElementsByTagNameNS($officeNs, 'spreadsheet');
foreach ($spreadsheets as $workbookData) {
/** @var \DOMElement $workbookData */
$tables = $workbookData->getElementsByTagNameNS($tableNs, "table");
$tables = $workbookData->getElementsByTagNameNS($tableNs, 'table');
$worksheetID = 0;
foreach ($tables as $worksheetDataSet) {
/** @var \DOMElement $worksheetDataSet */
$worksheetName = $worksheetDataSet->getAttributeNS($tableNs, "name");
$worksheetName = $worksheetDataSet->getAttributeNS($tableNs, 'name');
// Check loadSheetsOnly
if (isset($this->loadSheetsOnly)
@ -435,7 +435,7 @@ class Ods extends BaseReader implements IReader
}
// Create sheet
if($worksheetID > 0){
if ($worksheetID > 0) {
$spreadsheet->createSheet(); // First sheet is added by default
}
$spreadsheet->setActiveSheetIndex($worksheetID);
@ -453,15 +453,15 @@ class Ods extends BaseReader implements IReader
/** @var \DOMElement $childNode */
// Filter elements which are not under the "table" ns
if($childNode->namespaceURI != $tableNs){
if ($childNode->namespaceURI != $tableNs) {
continue;
}
$key = $childNode->nodeName;
// Remove ns from node name
if(strpos($key, ":") !== false){
$keyChunks = explode(":", $key);
if (strpos($key, ':') !== false) {
$keyChunks = explode(':', $key);
$key = array_pop($keyChunks);
}
@ -478,16 +478,15 @@ class Ods extends BaseReader implements IReader
break;
case 'table-row':
if($childNode->hasAttributeNS($tableNs, 'number-rows-repeated')){
if ($childNode->hasAttributeNS($tableNs, 'number-rows-repeated')) {
$rowRepeats = $childNode->getAttributeNS($tableNs, 'number-rows-repeated');
}
else{
} else {
$rowRepeats = 1;
}
$columnID = 'A';
foreach ($childNode->childNodes as $key => $cellData) {
/** @var \DOMElement $cellData */
/* @var \DOMElement $cellData */
if ($this->getReadFilter() !== null) {
if (!$this->getReadFilter()->readCell($columnID, $rowID, $worksheetName)) {
@ -501,19 +500,18 @@ class Ods extends BaseReader implements IReader
$hasCalculatedValue = false;
$cellDataFormula = '';
if ($cellData->hasAttributeNS($tableNs, "formula")) {
$cellDataFormula = $cellData->getAttributeNS($tableNs, "formula");
if ($cellData->hasAttributeNS($tableNs, 'formula')) {
$cellDataFormula = $cellData->getAttributeNS($tableNs, 'formula');
$hasCalculatedValue = true;
}
// Annotations
$annotation = $cellData->getElementsByTagNameNS($officeNs, "annotation");
$annotation = $cellData->getElementsByTagNameNS($officeNs, 'annotation');
if ($annotation->length > 0) {
$textNode = $annotation->item(0)->getElementsByTagNameNS($textNs, "p");
if($textNode->length > 0){
$textNode = $annotation->item(0)->getElementsByTagNameNS($textNs, 'p');
if ($textNode->length > 0) {
$text = $this->scanElementForText($textNode->item(0));
$spreadsheet->getActiveSheet()
@ -532,13 +530,12 @@ class Ods extends BaseReader implements IReader
/** @var \DOMElement $item */
// Filter text:p elements
if($item->nodeName == "text:p"){
if ($item->nodeName == 'text:p') {
$paragraphs[] = $item;
}
}
if (count($paragraphs) > 0) {
// Consolidate if there are multiple p records (maybe with spans as well)
$dataArray = [];
@ -560,9 +557,9 @@ class Ods extends BaseReader implements IReader
$dataValue = $allCellDataText;
foreach ($paragraphs as $paragraph) {
$link = $paragraph->getElementsByTagNameNS($textNs, "a");
if($link->length > 0){
$hyperlink = $link->item(0)->getAttributeNS($xlinkNs, "href");
$link = $paragraph->getElementsByTagNameNS($textNs, 'a');
if ($link->length > 0) {
$hyperlink = $link->item(0)->getAttributeNS($xlinkNs, 'href');
}
}
@ -573,7 +570,7 @@ class Ods extends BaseReader implements IReader
break;
case 'percentage':
$type = DataType::TYPE_NUMERIC;
$dataValue = (float)$cellData->getAttributeNS($officeNs, 'value');
$dataValue = (float) $cellData->getAttributeNS($officeNs, 'value');
if (floor($dataValue) == $dataValue) {
$dataValue = (int) $dataValue;
@ -582,7 +579,7 @@ class Ods extends BaseReader implements IReader
break;
case 'currency':
$type = DataType::TYPE_NUMERIC;
$dataValue = (float)$cellData->getAttributeNS($officeNs, 'value');
$dataValue = (float) $cellData->getAttributeNS($officeNs, 'value');
if (floor($dataValue) == $dataValue) {
$dataValue = (int) $dataValue;
@ -591,7 +588,7 @@ class Ods extends BaseReader implements IReader
break;
case 'float':
$type = DataType::TYPE_NUMERIC;
$dataValue = (float)$cellData->getAttributeNS($officeNs, 'value');
$dataValue = (float) $cellData->getAttributeNS($officeNs, 'value');
if (floor($dataValue) == $dataValue) {
if ($dataValue == (int) $dataValue) {
@ -635,7 +632,6 @@ class Ods extends BaseReader implements IReader
);
$formatting = \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_TIME4;
break;
default:
$dataValue = null;
}
@ -650,10 +646,8 @@ class Ods extends BaseReader implements IReader
$temp = explode('"', $cellDataFormula);
$tKey = false;
foreach ($temp as &$value) {
// Only replace in alternate array entries (i.e. non-quoted blocks)
if ($tKey = !$tKey) {
// Cell range reference in another sheet
$value = preg_replace('/\[([^\.]+)\.([^\.]+):\.([^\.]+)\]/Ui', '$1!$2:$3', $value);
@ -675,16 +669,14 @@ class Ods extends BaseReader implements IReader
$cellDataFormula = implode('"', $temp);
}
if($cellData->hasAttributeNS($tableNs, 'number-columns-repeated')){
$colRepeats = (int)$cellData->getAttributeNS($tableNs, 'number-columns-repeated');
}
else{
if ($cellData->hasAttributeNS($tableNs, 'number-columns-repeated')) {
$colRepeats = (int) $cellData->getAttributeNS($tableNs, 'number-columns-repeated');
} else {
$colRepeats = 1;
}
if ($type !== null) {
for ($i = 0; $i < $colRepeats; ++$i) {
if ($i > 0) {
++$columnID;
}
@ -697,10 +689,9 @@ class Ods extends BaseReader implements IReader
->getCell($columnID . $rID);
// Set value
if($hasCalculatedValue){
if ($hasCalculatedValue) {
$cell->setValueExplicit($cellDataFormula, $type);
}
else{
} else {
$cell->setValueExplicit($dataValue, $type);
}
@ -738,9 +729,8 @@ class Ods extends BaseReader implements IReader
$columnTo = $columnID;
if ($cellData->hasAttributeNS($tableNs, 'number-columns-spanned')) {
$columnIndex = \PhpOffice\PhpSpreadsheet\Cell::columnIndexFromString($columnID);
$columnIndex += (int)$cellData->getAttributeNS($tableNs, 'number-columns-spanned');
$columnIndex += (int) $cellData->getAttributeNS($tableNs, 'number-columns-spanned');
$columnIndex -= 2;
$columnTo = \PhpOffice\PhpSpreadsheet\Cell::stringFromColumnIndex($columnIndex);
@ -749,7 +739,7 @@ class Ods extends BaseReader implements IReader
$rowTo = $rowID;
if ($cellData->hasAttributeNS($tableNs, 'number-rows-spanned')) {
$rowTo = $rowTo + (int)$cellData->getAttributeNS($tableNs, 'number-rows-spanned') - 1;
$rowTo = $rowTo + (int) $cellData->getAttributeNS($tableNs, 'number-rows-spanned') - 1;
}
$cellRange = $columnID . $rowID . ':' . $columnTo . $rowTo;
@ -772,38 +762,35 @@ class Ods extends BaseReader implements IReader
}
/**
* Recursively scan element
* Recursively scan element.
*
* @param \DOMNode $element
*
* @return string
*/
protected function scanElementForText(\DOMNode $element){
$str = "";
foreach($element->childNodes as $child){
protected function scanElementForText(\DOMNode $element)
{
$str = '';
foreach ($element->childNodes as $child) {
/** @var \DOMNode $child */
if($child->nodeType == XML_TEXT_NODE){
if ($child->nodeType == XML_TEXT_NODE) {
$str .= $child->nodeValue;
}
elseif($child->nodeType == XML_ELEMENT_NODE && $child->nodeName == "text:s"){
} elseif ($child->nodeType == XML_ELEMENT_NODE && $child->nodeName == 'text:s') {
// It's a space
// Multiple spaces?
if(isset($child->attributes["text:c"])){
if (isset($child->attributes['text:c'])) {
/** @var \DOMAttr $cAttr */
$cAttr = $child->attributes["text:c"];
$multiplier = (int)$cAttr->nodeValue;
}
else{
$cAttr = $child->attributes['text:c'];
$multiplier = (int) $cAttr->nodeValue;
} else {
$multiplier = 1;
}
$str .= str_repeat(" ", $multiplier);
$str .= str_repeat(' ', $multiplier);
}
if($child->hasChildNodes()){
if ($child->hasChildNodes()) {
$str .= $this->scanElementForText($child);
}
}
@ -813,6 +800,7 @@ class Ods extends BaseReader implements IReader
/**
* @param string $is
*
* @return \PhpOffice\PhpSpreadsheet\RichText
*/
private function parseRichText($is = '')

View File

@ -24,9 +24,9 @@ class OdsTest extends \PHPUnit_Framework_TestCase
/**
* @return \PhpOffice\PhpSpreadsheet\Spreadsheet
*/
protected function loadOOCalcTestFile(){
if(!$this->spreadsheetOOCalcTest){
protected function loadOOCalcTestFile()
{
if (!$this->spreadsheetOOCalcTest) {
$filename = __DIR__ . '/../../../samples/templates/OOCalcTest.ods';
// Load into this instance
@ -40,9 +40,9 @@ class OdsTest extends \PHPUnit_Framework_TestCase
/**
* @return \PhpOffice\PhpSpreadsheet\Spreadsheet
*/
protected function loadDataFile(){
if(!$this->spreadsheetData){
protected function loadDataFile()
{
if (!$this->spreadsheetData) {
$filename = __DIR__ . '/../../data/Reader/Ods/data.ods';
// Load into this instance
@ -63,8 +63,8 @@ class OdsTest extends \PHPUnit_Framework_TestCase
// Test "listWorksheetNames" method
$this->assertEquals([
"Sheet1",
"Second Sheet",
'Sheet1',
'Second Sheet',
], $reader->listWorksheetNames($filename));
}
@ -83,8 +83,8 @@ class OdsTest extends \PHPUnit_Framework_TestCase
$this->assertInstanceOf('PhpOffice\PhpSpreadsheet\Worksheet', $secondSheet);
}
public function testReadValueAndComments(){
public function testReadValueAndComments()
{
$spreadsheet = $this->loadOOCalcTestFile();
$firstSheet = $spreadsheet->getSheet(0);
@ -93,41 +93,41 @@ class OdsTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('N', $firstSheet->getHighestColumn());
// Simple cell value
$this->assertEquals("Test String 1", $firstSheet->getCell("A1")->getValue());
$this->assertEquals('Test String 1', $firstSheet->getCell('A1')->getValue());
// Merged cell
$this->assertEquals("BOX", $firstSheet->getCell("B18")->getValue());
$this->assertEquals('BOX', $firstSheet->getCell('B18')->getValue());
// Comments/Annotations
$this->assertEquals(
"Test for a simple colour-formatted string",
$firstSheet->getComment("A1")->getText()->getPlainText()
'Test for a simple colour-formatted string',
$firstSheet->getComment('A1')->getText()->getPlainText()
);
// Data types
$this->assertEquals(DataType::TYPE_STRING, $firstSheet->getCell("A1")->getDataType());
$this->assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell("B1")->getDataType()); // Int
$this->assertEquals(DataType::TYPE_STRING, $firstSheet->getCell('A1')->getDataType());
$this->assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('B1')->getDataType()); // Int
$this->assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell("B6")->getDataType()); // Float
$this->assertEquals(1.23, $firstSheet->getCell("B6")->getValue());
$this->assertEquals(0, $firstSheet->getCell("G10")->getValue());
$this->assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('B6')->getDataType()); // Float
$this->assertEquals(1.23, $firstSheet->getCell('B6')->getValue());
$this->assertEquals(0, $firstSheet->getCell('G10')->getValue());
$this->assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell("A10")->getDataType()); // Date
$this->assertEquals(22269.0, $firstSheet->getCell("A10")->getValue());
$this->assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A10')->getDataType()); // Date
$this->assertEquals(22269.0, $firstSheet->getCell('A10')->getValue());
$this->assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell("A13")->getDataType()); // Time
$this->assertEquals(25569.0625, $firstSheet->getCell("A13")->getValue());
$this->assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A13')->getDataType()); // Time
$this->assertEquals(25569.0625, $firstSheet->getCell('A13')->getValue());
$this->assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell("A15")->getDataType()); // Date + Time
$this->assertEquals(22269.0625, $firstSheet->getCell("A15")->getValue());
$this->assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A15')->getDataType()); // Date + Time
$this->assertEquals(22269.0625, $firstSheet->getCell('A15')->getValue());
$this->assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell("A11")->getDataType()); // Fraction
$this->assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A11')->getDataType()); // Fraction
$this->assertEquals(DataType::TYPE_BOOL, $firstSheet->getCell("D6")->getDataType());
$this->assertTrue($firstSheet->getCell("D6")->getValue());
$this->assertEquals(DataType::TYPE_BOOL, $firstSheet->getCell('D6')->getDataType());
$this->assertTrue($firstSheet->getCell('D6')->getValue());
$this->assertEquals(DataType::TYPE_FORMULA, $firstSheet->getCell("C6")->getDataType()); // Formula
$this->assertEquals("=TRUE()", $firstSheet->getCell("C6")->getValue()); // Formula
$this->assertEquals(DataType::TYPE_FORMULA, $firstSheet->getCell('C6')->getDataType()); // Formula
$this->assertEquals('=TRUE()', $firstSheet->getCell('C6')->getValue()); // Formula
/*
* Percentage, Currency
@ -137,17 +137,17 @@ class OdsTest extends \PHPUnit_Framework_TestCase
$firstSheet = $spreadsheet->getSheet(0);
$this->assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell("A1")->getDataType()); // Percentage (10%)
$this->assertEquals(0.1, $firstSheet->getCell("A1")->getValue());
$this->assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A1')->getDataType()); // Percentage (10%)
$this->assertEquals(0.1, $firstSheet->getCell('A1')->getValue());
$this->assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell("A2")->getDataType()); // Percentage (10.00%)
$this->assertEquals(0.1, $firstSheet->getCell("A2")->getValue());
$this->assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A2')->getDataType()); // Percentage (10.00%)
$this->assertEquals(0.1, $firstSheet->getCell('A2')->getValue());
$this->assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell("A4")->getDataType()); // Currency (€10.00)
$this->assertEquals(10, $firstSheet->getCell("A4")->getValue());
$this->assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A4')->getDataType()); // Currency (€10.00)
$this->assertEquals(10, $firstSheet->getCell('A4')->getValue());
$this->assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell("A5")->getDataType()); // Currency ($20)
$this->assertEquals(20, $firstSheet->getCell("A5")->getValue());
$this->assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A5')->getDataType()); // Currency ($20)
$this->assertEquals(20, $firstSheet->getCell('A5')->getValue());
}
public function testReadColors()
@ -157,47 +157,46 @@ class OdsTest extends \PHPUnit_Framework_TestCase
// Background color
$style = $firstSheet->getCell("K3")->getStyle();
$style = $firstSheet->getCell('K3')->getStyle();
$this->assertEquals("none", $style->getFill()->getFillType());
$this->assertEquals("FFFFFFFF", $style->getFill()->getStartColor()->getARGB());
$this->assertEquals("FF000000", $style->getFill()->getEndColor()->getARGB());
$this->assertEquals('none', $style->getFill()->getFillType());
$this->assertEquals('FFFFFFFF', $style->getFill()->getStartColor()->getARGB());
$this->assertEquals('FF000000', $style->getFill()->getEndColor()->getARGB());
}
public function testReadRichText(){
public function testReadRichText()
{
$spreadsheet = $this->loadOOCalcTestFile();
$firstSheet = $spreadsheet->getSheet(0);
$this->assertEquals(
"I don't know if OOCalc supports Rich Text in the same way as Excel, " .
"And this row should be autofit height with text wrap",
$firstSheet->getCell("A28")->getValue()
'And this row should be autofit height with text wrap',
$firstSheet->getCell('A28')->getValue()
);
}
public function testReadCellsWithRepeatedSpaces(){
public function testReadCellsWithRepeatedSpaces()
{
$spreadsheet = $this->loadDataFile();
$firstSheet = $spreadsheet->getSheet(0);
$this->assertEquals("This has 4 spaces before and 2 after ", $firstSheet->getCell("A8")->getValue());
$this->assertEquals("This only one after ", $firstSheet->getCell("A9")->getValue());
$this->assertEquals("Test with DIFFERENT styles and multiple spaces: ", $firstSheet->getCell("A10")->getValue());
$this->assertEquals("test with new \nLines", $firstSheet->getCell("A11")->getValue());
$this->assertEquals('This has 4 spaces before and 2 after ', $firstSheet->getCell('A8')->getValue());
$this->assertEquals('This only one after ', $firstSheet->getCell('A9')->getValue());
$this->assertEquals('Test with DIFFERENT styles and multiple spaces: ', $firstSheet->getCell('A10')->getValue());
$this->assertEquals("test with new \nLines", $firstSheet->getCell('A11')->getValue());
}
public function testReadHyperlinks(){
public function testReadHyperlinks()
{
$spreadsheet = $this->loadOOCalcTestFile();
$firstSheet = $spreadsheet->getSheet(0);
$hyperlink = $firstSheet->getCell("A29");
$hyperlink = $firstSheet->getCell('A29');
$this->assertEquals(DataType::TYPE_STRING, $hyperlink->getDataType());
$this->assertEquals("PHPExcel", $hyperlink->getValue());
$this->assertEquals("http://www.phpexcel.net/", $hyperlink->getHyperlink()->getUrl());
$this->assertEquals('PHPExcel', $hyperlink->getValue());
$this->assertEquals('http://www.phpexcel.net/', $hyperlink->getHyperlink()->getUrl());
}
/*
@ -206,22 +205,22 @@ class OdsTest extends \PHPUnit_Framework_TestCase
public function testReadBoldItalicUnderline()
{
$this->markTestSkipped("Features not implemented yet");
$this->markTestSkipped('Features not implemented yet');
$spreadsheet = $this->loadOOCalcTestFile();
$firstSheet = $spreadsheet->getSheet(0);
// Font styles
$style = $firstSheet->getCell("A1")->getStyle();
$this->assertEquals("FF000000", $style->getFont()->getColor()->getARGB());
$style = $firstSheet->getCell('A1')->getStyle();
$this->assertEquals('FF000000', $style->getFont()->getColor()->getARGB());
$this->assertEquals(11, $style->getFont()->getSize());
$this->assertEquals(Font::UNDERLINE_NONE, $style->getFont()->getUnderline());
$style = $firstSheet->getCell("E3")->getStyle();
$style = $firstSheet->getCell('E3')->getStyle();
$this->assertEquals(Font::UNDERLINE_SINGLE, $style->getFont()->getUnderline());
$style = $firstSheet->getCell("E1")->getStyle();
$style = $firstSheet->getCell('E1')->getStyle();
$this->assertTrue($style->getFont()->getBold());
$this->assertTrue($style->getFont()->getItalic());
}