mirror of
https://github.com/php/doc-es.git
synced 2026-03-23 23:12:09 +01:00
Add NoDiscard attribute documentation (#367)
* Add NoDiscard attribute documentation * Remove versions.xml (DO NOT TRANSLATE file)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- EN-Revision: e890e4a7f97a9ea85e60a38443e7c8eb7ae9383f Maintainer: PhilDaiguille Status: ready -->
|
||||
<!-- EN-Revision: 02bee41067ab2822cbffcb4b3b2387f79488dffd Maintainer: PhilDaiguille Status: ready -->
|
||||
<!-- Reviewed: no -->
|
||||
<part xml:id="reserved.attributes" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<title>Atributos predefinidos</title>
|
||||
@@ -13,6 +13,7 @@
|
||||
&language.predefined.attributes.attribute;
|
||||
&language.predefined.attributes.allowdynamicproperties;
|
||||
&language.predefined.attributes.deprecated;
|
||||
&language.predefined.attributes.nodiscard;
|
||||
&language.predefined.attributes.override;
|
||||
&language.predefined.attributes.returntypewillchange;
|
||||
&language.predefined.attributes.sensitiveparameter;
|
||||
|
||||
179
language/predefined/attributes/nodiscard.xml
Normal file
179
language/predefined/attributes/nodiscard.xml
Normal file
@@ -0,0 +1,179 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- EN-Revision: 02bee41067ab2822cbffcb4b3b2387f79488dffd Maintainer: PhilDaiguille Status: ready -->
|
||||
<!-- Reviewed: no -->
|
||||
<reference xml:id="class.nodiscard" role="class" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xi="http://www.w3.org/2001/XInclude">
|
||||
<title>El atributo NoDiscard</title>
|
||||
<titleabbrev>NoDiscard</titleabbrev>
|
||||
|
||||
<partintro>
|
||||
|
||||
<section xml:id="nodiscard.intro">
|
||||
&reftitle.intro;
|
||||
<simpara>
|
||||
Este atributo se puede utilizar para indicar que el valor de retorno de una función
|
||||
o de un método no debe ser descartado. Si el valor de retorno no se utiliza de
|
||||
ninguna manera, se emitirá una advertencia.
|
||||
</simpara>
|
||||
<simpara>
|
||||
Esto es útil para funciones en las que no comprobar el valor de retorno es
|
||||
probablemente un error.
|
||||
</simpara>
|
||||
<simpara>
|
||||
Para descartar intencionalmente el valor de retorno de dicha función, utilice
|
||||
la conversión (void) para suprimir la advertencia.
|
||||
</simpara>
|
||||
<note>
|
||||
<simpara>
|
||||
Dado que los atributos están diseñados para ser retrocompatibles,
|
||||
<code>#[\NoDiscard]</code> se puede añadir a funciones y métodos
|
||||
incluso cuando se soportan PHP 8.4 o versiones anteriores, simplemente no hará nada.
|
||||
En PHP 8.5 y superiores se emitirá una advertencia si el resultado no se utiliza.
|
||||
Para suprimir la advertencia sin usar <code>(void)</code>,
|
||||
que no es soportado antes de PHP 8.5,
|
||||
considere usar una variable como <code>$_</code>.
|
||||
</simpara>
|
||||
</note>
|
||||
</section>
|
||||
|
||||
<section xml:id="nodiscard.synopsis">
|
||||
&reftitle.classsynopsis;
|
||||
|
||||
<classsynopsis class="class">
|
||||
<ooclass>
|
||||
<modifier>final</modifier>
|
||||
<classname>NoDiscard</classname>
|
||||
</ooclass>
|
||||
|
||||
<classsynopsisinfo role="comment">&Properties;</classsynopsisinfo>
|
||||
<fieldsynopsis>
|
||||
<modifier>public</modifier>
|
||||
<modifier>readonly</modifier>
|
||||
<type class="union"><type>string</type><type>null</type></type>
|
||||
<varname linkend="nodiscard.props.message">message</varname>
|
||||
</fieldsynopsis>
|
||||
|
||||
<classsynopsisinfo role="comment">&Methods;</classsynopsisinfo>
|
||||
<xi:include xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(id('class.nodiscard')/db:refentry/db:refsect1[@role='description']/descendant::db:constructorsynopsis[@role='NoDiscard'])">
|
||||
<xi:fallback/>
|
||||
</xi:include>
|
||||
</classsynopsis>
|
||||
|
||||
</section>
|
||||
|
||||
<section xml:id="nodiscard.props">
|
||||
&reftitle.properties;
|
||||
<variablelist>
|
||||
<varlistentry xml:id="nodiscard.props.message">
|
||||
<term><varname>message</varname></term>
|
||||
<listitem>
|
||||
<simpara>
|
||||
Un mensaje opcional que explica por qué el valor de retorno no debe ser descartado.
|
||||
</simpara>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
</variablelist>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
&reftitle.examples;
|
||||
<example>
|
||||
<title>Uso básico</title>
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Processes all the given items and returns an array with the results of the
|
||||
* operation for each item. `null` indicates success and an Exception indicates
|
||||
* an error. The keys of the result array match the keys of the $items array.
|
||||
*
|
||||
* @param array<string> $items
|
||||
* @return array<null|Exception>
|
||||
*/
|
||||
#[\NoDiscard("as processing might fail for individual items")]
|
||||
function bulk_process(array $items): array {
|
||||
$results = [];
|
||||
|
||||
foreach ($items as $key => $item) {
|
||||
if (\random_int(0, 9999) < 9999) {
|
||||
// Pretend to do something useful with $item,
|
||||
// which will succeed in 99.99% of cases.
|
||||
echo "Processing {$item}", PHP_EOL;
|
||||
$error = null;
|
||||
} else {
|
||||
$error = new \Exception("Failed to process {$item}.");
|
||||
}
|
||||
|
||||
$results[$key] = $error;
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
bulk_process($items);
|
||||
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
&example.outputs.85.similar;
|
||||
<screen>
|
||||
<![CDATA[
|
||||
Warning: The return value of function bulk_process() should either be used or intentionally ignored by casting it as (void), as processing might fail for individual items
|
||||
]]>
|
||||
</screen>
|
||||
</example>
|
||||
<example>
|
||||
<title>Descartar intencionalmente el valor de retorno</title>
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
|
||||
#[\NoDiscard]
|
||||
function some_command(): int {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Suprimir la advertencia usando (void) - PHP 8.5+
|
||||
(void) some_command();
|
||||
|
||||
// Para compatibilidad con versiones de PHP anteriores a 8.5, usar una variable temporal
|
||||
$_ = some_command();
|
||||
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
</example>
|
||||
</section>
|
||||
|
||||
<section xml:id="nodiscard.seealso">
|
||||
&reftitle.seealso;
|
||||
<simplelist>
|
||||
<member><link linkend="language.attributes">Attributes overview</link></member>
|
||||
</simplelist>
|
||||
</section>
|
||||
|
||||
</partintro>
|
||||
|
||||
&language.predefined.attributes.nodiscard.construct;
|
||||
|
||||
</reference>
|
||||
<!-- Keep this comment at the end of the file
|
||||
Local variables:
|
||||
mode: sgml
|
||||
sgml-omittag:t
|
||||
sgml-shorttag:t
|
||||
sgml-minimize-attributes:nil
|
||||
sgml-always-quote-attributes:t
|
||||
sgml-indent-step:1
|
||||
sgml-indent-data:t
|
||||
indent-tabs-mode:nil
|
||||
sgml-parent-document:nil
|
||||
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
||||
sgml-exposed-tags:nil
|
||||
sgml-local-catalogs:nil
|
||||
sgml-local-ecat-files:nil
|
||||
End:
|
||||
vim600: syn=xml fen fdm=syntax fdl=2 si
|
||||
vim: et tw=78 syn=sgml
|
||||
vi: ts=1 sw=1
|
||||
-->
|
||||
54
language/predefined/attributes/nodiscard/construct.xml
Normal file
54
language/predefined/attributes/nodiscard/construct.xml
Normal file
@@ -0,0 +1,54 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- EN-Revision: 02bee41067ab2822cbffcb4b3b2387f79488dffd Maintainer: PhilDaiguille Status: ready -->
|
||||
<!-- Reviewed: no -->
|
||||
<refentry xml:id="nodiscard.construct" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<refnamediv>
|
||||
<refname>NoDiscard::__construct</refname>
|
||||
<refpurpose>Construye una nueva instancia del atributo NoDiscard</refpurpose>
|
||||
</refnamediv>
|
||||
|
||||
<refsect1 role="description">
|
||||
&reftitle.description;
|
||||
<constructorsynopsis role="NoDiscard">
|
||||
<modifier>public</modifier> <methodname>NoDiscard::__construct</methodname>
|
||||
<methodparam choice="opt"><type class="union"><type>string</type><type>null</type></type><parameter>message</parameter><initializer>&null;</initializer></methodparam>
|
||||
</constructorsynopsis>
|
||||
<simpara>
|
||||
Construye una nueva instancia de <classname>NoDiscard</classname>.
|
||||
</simpara>
|
||||
</refsect1>
|
||||
|
||||
<refsect1 role="parameters">
|
||||
&reftitle.parameters;
|
||||
<variablelist>
|
||||
<varlistentry>
|
||||
<term><parameter>message</parameter></term>
|
||||
<listitem>
|
||||
<simpara>
|
||||
El valor de la propiedad <property linkend="nodiscard.props.message">message</property>.
|
||||
</simpara>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
</variablelist>
|
||||
</refsect1>
|
||||
</refentry>
|
||||
<!-- Keep this comment at the end of the file
|
||||
Local variables:
|
||||
mode: sgml
|
||||
sgml-omittag:t
|
||||
sgml-shorttag:t
|
||||
sgml-minimize-attributes:nil
|
||||
sgml-always-quote-attributes:t
|
||||
sgml-indent-step:1
|
||||
sgml-indent-data:t
|
||||
indent-tabs-mode:nil
|
||||
sgml-parent-document:nil
|
||||
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
||||
sgml-exposed-tags:nil
|
||||
sgml-local-catalogs:nil
|
||||
sgml-local-ecat-files:nil
|
||||
End:
|
||||
vim600: syn=xml fen fdm=syntax fdl=2 si
|
||||
vim: et tw=78 syn=sgml
|
||||
vi: ts=1 sw=1
|
||||
-->
|
||||
Reference in New Issue
Block a user