Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read hyperlink for drawing image #490

Closed
wants to merge 19 commits into from
Closed

read hyperlink for drawing image #490

wants to merge 19 commits into from

Conversation

tezrik
Copy link
Contributor

@tezrik tezrik commented May 7, 2018

This is:

- [ ] a bugfix
- [X] a new feature

Checklist:

Why this change is needed?

Read hyperlinks for drawing image

Copy link
Member

@PowerKiKi PowerKiKi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like an interesting contribution, but it misses a few things to be merged. Could you take care of it ?

  • add write support for symetry
  • document feature in docs/references/features-cross-reference.md
  • cover with unit tests (see EnclosureTest example)

@tezrik
Copy link
Contributor Author

tezrik commented May 7, 2018

sorry in my English
Ok, I'll try to do it in the near future.

@tezrik
Copy link
Contributor Author

tezrik commented May 14, 2018

I think I had a problem. I can not understand the error. Please help me

@criztovyl
Copy link
Contributor

The error is that some of the methods you changed no longer pass code quality checks.

@@ -179,6 +185,15 @@ public function writeDrawing(XMLWriter $objWriter, BaseDrawing $pDrawing, $pRela
$objWriter->writeAttribute('id', $pRelationId);
$objWriter->writeAttribute('name', $pDrawing->getName());
$objWriter->writeAttribute('descr', $pDrawing->getDescription());

//a:hlinkClick
if ($pHlinkClickId >= 0) {
Copy link
Contributor

@criztovyl criztovyl May 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if increases the "Condition count" from 6 to 7, which raised the rating from B to C.
The project's current configuration of Scrutinizer forbids ratings above or equal to C.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, the if should be extract to new private method.

$drawing->getHyperlink()->getUrl(),
$drawing->getHyperlink()->isInternal() ? '' : 'External'
);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The if and the ?: here raise the "Condition count" from 8 to 10 and together with the added lines themselves raised the rating from B to C.
Scrutinizer about Rels::writeDrawingRelationships().

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scrutinizer has some suggestions how to fix this particular issue.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again the entire if should go into a new private method (even if it's short). Something like writeDrawingHyperLink(XMLWriter $objWriter, BaseDrawing $drawing)

@criztovyl
Copy link
Contributor

If you are not sure how to fix the issues, wait for PowerKiki's comments about your further changes.

@tezrik
Copy link
Contributor Author

tezrik commented May 15, 2018

Thank you for the clarification. I expect then a response from PowerKiki's

Copy link
Member

@PowerKiKi PowerKiKi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks you for the modifications. The test and doc look good. There is a few things that can improve the code quality before merging. Please have a look.

require __DIR__ . '/../Header.php';

$inputFileType = 'Xlsx';
$inputFileName = __DIR__ . '/sampleData/example3.xlsx';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We try to avoid adding XLSX files when possible to avoid growing the size of the repository. So please delete samples/Reader/sampleData/example3.xlsx, and either change the sample to write and read, or delete the sample entirely (but do not keep it with only read).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has not been addressed.

@@ -1606,6 +1613,14 @@ public function load($pFilename)
$shadow->getColor()->setRGB(self::getArrayItem($outerShdw->srgbClr->attributes(), 'val'));
$shadow->setAlpha(self::getArrayItem($outerShdw->srgbClr->alpha->attributes(), 'val') / 1000);
}
if ($hlinkClick) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if must be extracted to a separate private method.

@@ -1654,6 +1670,14 @@ public function load($pFilename)
$shadow->getColor()->setRGB(self::getArrayItem($outerShdw->srgbClr->attributes(), 'val'));
$shadow->setAlpha(self::getArrayItem($outerShdw->srgbClr->alpha->attributes(), 'val') / 1000);
}
if ($hlinkClick) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idem, extract to method.

/**
* Image hyperlink.
*
* @var Hyperlink
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be @var null|Hyperlink

@@ -508,4 +516,24 @@ public function __clone()
}
}
}

/**
* @param Hyperlink $pHyperlink
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be @var null|Hyperlink

public function getHyperlink()
{
if ($this->hyperlink === null) {
$this->hyperlink = new Hyperlink();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you create new Hyperlink if it does not exist, instead of returning null ?

@@ -179,6 +185,15 @@ public function writeDrawing(XMLWriter $objWriter, BaseDrawing $pDrawing, $pRela
$objWriter->writeAttribute('id', $pRelationId);
$objWriter->writeAttribute('name', $pDrawing->getName());
$objWriter->writeAttribute('descr', $pDrawing->getDescription());

//a:hlinkClick
if ($pHlinkClickId >= 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, the if should be extract to new private method.

*
* @throws WriterException
*/
public function writeDrawing(XMLWriter $objWriter, BaseDrawing $pDrawing, $pRelationId = -1)
public function writeDrawing(XMLWriter $objWriter, BaseDrawing $pDrawing, $pRelationId = -1, $pHlinkClickId = -1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use null instead of -1 for a better semantic of absence of value. Then we should also change @param null|int $pHlinkClickId.

$drawing->getHyperlink()->getUrl(),
$drawing->getHyperlink()->isInternal() ? '' : 'External'
);
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again the entire if should go into a new private method (even if it's short). Something like writeDrawingHyperLink(XMLWriter $objWriter, BaseDrawing $drawing)

* User: yuzhakov
* Date: 08.05.18
* Time: 12:00.
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not use file comment blocks, please remove. You will be credited in the commit information.

@tezrik
Copy link
Contributor Author

tezrik commented May 25, 2018

Ok, I'll try to do it in the near future.

@tezrik
Copy link
Contributor Author

tezrik commented Jun 19, 2018

Sorry, but I can not understand how these tests are related to me. In my changes there are none.

Copy link
Member

@PowerKiKi PowerKiKi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are still a few things to change in code.

I restarted the test job, let's see if it passes...

require __DIR__ . '/../Header.php';

$inputFileType = 'Xlsx';
$inputFileName = __DIR__ . '/sampleData/example3.xlsx';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has not been addressed.

<td></td>
<td></td>
<td></td>
<td>$Drawing->getHyperlink()->getUrl($url)</td>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"$Drawing" should be "$drawing"

*/
public function getTypeHyperlink()
{
return ($this->isInternal()) ? '' : 'External';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra parenthesis should be removed

* @param \SimpleXMLElement $cellAnchor
* @param array $hyperlinks
*/
private function readHyperLinkDrawing(&$objDrawing, $cellAnchor, $hyperlinks)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parameters must be type hinted. Also "&$objDrawing" must be "$objDrawing"

private function readHyperLinkDrawing(&$objDrawing, $cellAnchor, $hyperlinks)
{
$hlinkClick = $cellAnchor->pic->nvPicPr->cNvPr->children('http://schemas.openxmlformats.org/drawingml/2006/main')->hlinkClick;
if ($hlinkClick) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inverse the condition to return early if there is no link

*/
private function writeHyperLinkDriwing(XMLWriter &$objWriter, $pHlinkClickId)
{
if ($pHlinkClickId !== null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inverse condition and return early if no link

*/
private function writeDrawingHyperLink($objWriter, $drawing, &$i)
{
if ($drawing->getHyperlink() !== null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inverse condition and return early if no link

*
* @throws WriterException
*/
private function writeDrawingHyperLink($objWriter, $drawing, &$i)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parameters should be typed and &$i should be avoided and instead use return value.


class DrawingImageHyperlinkTest extends AbstractFunctional
{
const BASE_URL = 'https://github.com/PHPOffice/PhpSpreadsheet';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a variable in the method to avoid public visibility.

@@ -1,19 +0,0 @@
<?php
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file and without_cell_reference.xlsx should probably not be deleted

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry

@tezrik
Copy link
Contributor Author

tezrik commented Jun 30, 2018

Hi, I ran tests on the local machine. All tests pass without errors.

@PowerKiKi PowerKiKi closed this in 17d4a54 Jul 15, 2018
@PowerKiKi
Copy link
Member

Thanks ! I merged it.

Dfred pushed a commit to Dfred/PhpSpreadsheet that referenced this pull request Nov 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

3 participants